Skip to content

Move hard-coded logic to generate C main out of the compiler and into druntime#10351

Merged
dlang-bot merged 1 commit intodlang:masterfrom
JinShil:entry
Aug 26, 2019
Merged

Move hard-coded logic to generate C main out of the compiler and into druntime#10351
dlang-bot merged 1 commit intodlang:masterfrom
JinShil:entry

Conversation

@JinShil
Copy link
Copy Markdown
Contributor

@JinShil JinShil commented Aug 26, 2019

This PR will not pass any tests until dlang/druntime#2759 is merged.

This PR is futher work towards making the D language and its runtime more pay-as-you-go and extensible to other emerging platforms and use cases. It is very similar to the work already done in these PRs...

... and congruent with the current efforts to convert D runtime hooks to templates.

This PR moves the hard-coded logic for entry point generation (e.g. C main and the logic necessary to initialize druntime and call D main) out of the compiler and into to druntime. Doing so removes some of the complexity in the compiler, and instead, leverages D's unique language features to achieve the same result.

With this PR, the C main to D main code path can be completely customized for any platform, compiler, compiler invocation (e.g. betterC), or other use case without requiring any changes to the compiler. And, because it is template-based, it does not require linking in druntime.

If a user's custom runtime does not include the _d_cmain template definition, no C main to D main logic will be generated. This is especially useful for some bare metal programming where a user may wish to provide their own startup code without having to adopt workarounds such as renaming their main function or implementing druntime stubs.

How it works:

  1. The compiler detects the existence of a D main function declaration (this hasn't changed; the compiler already does this).
  2. The existence of the D main function declaration triggers a check for the template _d_cmain which will typically exist in druntime (assuming the accompanying druntime PR is merged).
  3. If the _d_cmain template is defined, the compiler simply appends a mixin _d_cmain!(); statement to the module effectively injecting C main and the logic necessary to initialize druntime and call D main.
  4. If the _d_cmain template is not defined, no code is injected, and the user is free to implement their own startup logic however they prefer

I believe this implementation will provide much more flexibility for everyone, while accomodating for emerging use cases, without the need for workarounds or compromise.

If necessary, the _d_cmain template could even be modified to take compile-time arguments from the compiler front-end to further extend its capabilities.

This PR is an alternative to #10181.

cc @kinke @ibuclaw @jpf91 This changes the interface in compiler.h, hopefully for the better. It removes the genCmain function and the entrypoint module, delegating that logic instead to the runtime.

}

// Only mixin `_d_cmain` if it is defined
if (cmainTemplateExists())
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I suppose I could put this logic back into compiler.genCmain if that would be less disruptive.

@dlang-bot
Copy link
Copy Markdown
Contributor

Thanks for your pull request and interest in making D better, @JinShil! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

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.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub fetch digger
dub run digger -- build "master + dmd#10351"

@JinShil JinShil changed the title Remove hard-coded logic to generate C main out of the compiler and into druntime Move hard-coded logic to generate C main out of the compiler and into druntime Aug 26, 2019
@dlang-bot dlang-bot merged commit a01aa55 into dlang:master Aug 26, 2019
@JinShil JinShil deleted the entry branch August 26, 2019 11:53
@trikko
Copy link
Copy Markdown
Contributor

trikko commented Nov 8, 2019

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants