Skip to content

Conversation

@WalterBright
Copy link
Member

This uses the mechanism Digital Mars C++ uses to call C++ static constructors and destructors to call D shared static constructors and destructors in -betterC mode.

Each C compiler does it differently, so some research is necessary to figure it out for other platforms.

@dlang-bot
Copy link
Contributor

Thanks for your pull request, @WalterBright!

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

CyberShadow
CyberShadow previously approved these changes Jul 1, 2017
@@ -0,0 +1,21 @@
/* REQUIRED_ARGS: -betterC
* PERMUTE_ARGS:
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can TEST_OUTPUT be used here? Currently nothing tests that the ctors/dtors are actually executed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing the output will come when the other platforms are supported.

@jpf91
Copy link
Contributor

jpf91 commented Jul 1, 2017

For linux it's usually just placing a function pointer into a specific section.

👍 to the idea and more betterC work. One thing I was wondering though is that the betterC constructors then have slightly different runtime semantics (construction order is not guaranteed and cyclic module contructor dependencies therefore will be supported in betterC mode). I'm not sure if it is a good idea to change semantics with a compiler switch.

An alternative is to use an @CContructor UDA and normal module level functions, I think LDC already does that. Another benefit of the UDA approach is that you now can easily implement multiple named betterC constructors per module.

Ping @andralex as this is some kind of language change / discussion.

@WalterBright
Copy link
Member Author

Don't pull this yet, I have a better idea.

@WalterBright
Copy link
Member Author

Ok, better idea done.

Copy link
Member

@andralex andralex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool beans. This should come with a changelog entry and documentation. Please fix the bug and perhaps add more test (only ctor/dtor).

src/ddmd/glue.d Outdated
{
if (m.ssharedctor)
objmod.setModuleCtorDtor(m.ssharedctor, true);
if (m.ssharedctor)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BUG should be if (m.sshareddtor)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

danged copy/pasta errors!

@WalterBright WalterBright force-pushed the betterC-STI-STD branch 2 times, most recently from d8de3b7 to 78cbde8 Compare July 1, 2017 21:24
@WalterBright
Copy link
Member Author

@andralex done

@WalterBright
Copy link
Member Author

While this is waiting, there's #6957 for the non-controversial bits.

@ibuclaw
Copy link
Member

ibuclaw commented Jul 27, 2017

Perhaps -betterC should become a meta-flag for other features. Same as what is done for -release.

@jacob-carlborg
Copy link
Contributor

Is this the same as __attribute__((constructor)) on GCC/Clang? In that case I agree with @jpf91 that it should be a separate functionality, like a UDA or pragma on a regular function. This is a useful feature to have even when betterC is not used.

We also already have this for ELF targets (Linux and FreeBSD). It was added for the implementation of dynamic libraries.

Copy link
Member

@schveiguy schveiguy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm against such a change, since D users expect to have proper ordering of D static ctor/dtors regardless of link order. I would recommend instead to simply use the C compiler's feature with that compiler. I can't imagine chasing a bug due to linker order is going to be fun when you switch to -betterC mode.

Just giving my opinion for what it's worth, not expecting a response or acceptance.


Although D guarantees that static construction for imports occurs first,
no such ordering guarantee is available with -betterC. Typically, however, the order
matches the order in which object files are presente to the linker.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

presented?

@CyberShadow
Copy link
Member

I'm against such a change, since D users expect to have proper ordering of D static ctor/dtors regardless of link order.

Not having switches that subtly change language semantics is probably a good idea.

How about allowing the use of this feature through extern(C) static this() { ... } regardless of -betterC?

@schveiguy
Copy link
Member

schveiguy commented Oct 4, 2017

How about allowing the use of this feature through extern(C) static this() { ... } regardless of -betterC?

I don't like that either, because I guarantee there is some poor soul who put extern(C): at the top of their module, without putting extern(D) in front of their static ctor/dtor, and now you will drastically change the behavior.

FYI: there is a discussion about this very thing here: https://issues.dlang.org/show_bug.cgi?id=17747

@jacob-carlborg
Copy link
Contributor

It can be implemented using a UDA or pragma, like for GDC and LDC.

@MartinNowak
Copy link
Member

Each C compiler does it differently, so some research is necessary to figure it out for other platforms.

See #7182

@MartinNowak
Copy link
Member

We might close this in favor of #7182

For the unlikely need of a codebase to support both, D and D -betterC, version (D_betterC) could be used to switch between shared static this() and pragma(crt_constructor).

@andralex
Copy link
Member

Let's go with #7182. Further improvements will come on top of it.

@andralex andralex closed this Dec 12, 2017
@bitraft
Copy link

bitraft commented Dec 20, 2017

Hi, @andralex @WalterBright

please check on https://issues.dlang.org/show_bug.cgi?id=18100

crt_constructor need to allow set immutable(which mean it only should be call once, and can not been call from main function). If we simple use extern(C) shared static this will fix all problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants