Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgen: make .global.s work more according to spec #536

Merged
merged 1 commit into from
Feb 14, 2023

Conversation

zerbina
Copy link
Collaborator

@zerbina zerbina commented Feb 12, 2023

Summary

For the C target, globals defined inside procedures that are resource- like (i.e. have a user-defined or lifted destructor) are now initialized in a module's pre-init procedure, instead of each time control-flow reaches the definition. This mirrors what already happened for non-resource-like globals.

In addition, globals defined in inline procedures no longer cause linking errors because of duplicated definitions in the generated C code.

Details

The globals are extracted before translating the AST to MIR code, making the workaround in mirgen (that didn't work anyway) obsolete. In order to work towards unifying the architecture of the back-ends, vmbackend now also makes use of the pre-extraction, no longer requiring extra logic in the code-generator.

Known Issues

  • the destructor-injection pass is not run for the initializer expression of globals defined inside procedures
  • .global.s on the JS target still don't work according to specification

Notes for Reviewers

The PR is meant to unblock #450 with the least amount of work and changes to semantics. I think that the introduced regression (no destructor injection for initializers of procedure-level globals) is acceptable until the implementation gets an overhaul.

@zerbina zerbina added refactor Implementation refactor compiler General compiler tag compiler/backend Related to backend system of the compiler labels Feb 12, 2023
@zerbina zerbina added this to the C backend refactoring milestone Feb 12, 2023
of nkWhen:
# a ``when nimvm`` construct
# XXX: this logic duplciates what ``mirgen`` already does. Maybe
# collecting should happen there? Or procedure-level globals should
Copy link
Collaborator

@saem saem Feb 12, 2023

Choose a reason for hiding this comment

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

Likely makes sense in sem as a normalization step.

compiler/sem/transf.nim Outdated Show resolved Hide resolved
compiler/sem/transf.nim Outdated Show resolved Hide resolved
compiler/vm/vmbackend.nim Outdated Show resolved Hide resolved
@zerbina zerbina force-pushed the make-globals-work-according-to-spec branch from cceca8c to b67d177 Compare February 12, 2023 20:29
Copy link
Collaborator

@saem saem left a comment

Choose a reason for hiding this comment

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

Bors r+

Thanks for the detailed explanation, it was really easy to follow.

@bors
Copy link
Contributor

bors bot commented Feb 13, 2023

Merge conflict.

For the C target, globals defined inside procedures that are resource-
like (i.e. have a user-defined or lifted destructor) are now
initialized in a module's pre-init procedure, instead of each time
control-flow reaches the definition. This mirrors what already happened
for non-resource-like globals.

In addition, globals defined in `inline` procedures no longer cause
linking errors because of duplicated definitions in the generated C
code.

The globals are extracted before translating the AST to MIR code,
making the workaround in `mirgen` (that didn't work anyway) obsolete.
In order to work towards unifying the architecture of the back-ends,
`vmbackend` now also makes use of the pre-extraction, no longer
requiring extra logic in the code-generator.

- the destructor injection pass is not run for the initializer
  expression of globals defined inside procedures
- `.global.`s on the JS target still don't work according to
  specification
@zerbina zerbina force-pushed the make-globals-work-according-to-spec branch from b67d177 to b114b8d Compare February 13, 2023 20:05
@saem
Copy link
Collaborator

saem commented Feb 14, 2023

bors r+

@bors
Copy link
Contributor

bors bot commented Feb 14, 2023

Build succeeded:

@bors bors bot merged commit 5d1c185 into nim-works:devel Feb 14, 2023
@zerbina zerbina deleted the make-globals-work-according-to-spec branch February 14, 2023 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/backend Related to backend system of the compiler compiler General compiler tag refactor Implementation refactor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants