Skip to content

Conversation

zerbina
Copy link
Collaborator

@zerbina zerbina commented Oct 6, 2025

Summary

Only treat emit and asm statements at the module-scope and
outside of any complex expression as top-level emit/asm statements.

Details

The main goal is to move top-level emit/asm handling largely out of the
code generator and into the orchestrator; the user-facing behaviour
changing is a side effect.

When creating the module lifecycle routines from module AST, emit/asm
statements that are at the top-level are now extracted and put into a
separate section. They're then translated from AST to MIR and
emitted as the new bekEmit event by the backends.process iterator.

For the C backend, they're passed onto the code generator; they're not
possible for the other two backends. Section determination is removed
from cgen.

While the section modifiers are translated to the same C file section
as before, all top-level emit/asm statements are now processed before
other top-level code. Tests for top-level emit statements and those
using them are updated (where needed).

For the C target, they're no longer part of module "init" procedures.
They're handled by the `process` iterator, which emits an event for
every emit/asm statement.
Translating and emitting them is still done by `cgen`.
The section specifier wasn't at the start of the string, meaning it was
placed into the procedure section.
Also adjust the related specification test.
@zerbina zerbina added this to the C backend rework milestone Oct 6, 2025
@zerbina zerbina added refactor Implementation refactor compiler/backend Related to backend system of the compiler language-design Language design syntax, semantics, types, statics and dynamics. labels Oct 6, 2025
@zerbina
Copy link
Collaborator Author

zerbina commented Oct 6, 2025

I do think that emit sections are a misfeature (not only in their current incarnation, but in general), but given that they're still used by some tests without and there being a direct replacement, it was easier to just decouple the feature from cgen for now.

@saem
Copy link
Collaborator

saem commented Oct 7, 2025

/merge

Copy link

github-actions bot commented Oct 7, 2025

Merge requested by: @saem

Contents after the first section break of the PR description has been removed and preserved below:


Notes for Reviewers

@chore-runner chore-runner bot added this pull request to the merge queue Oct 7, 2025
Merged via the queue into nim-works:devel with commit b003450 Oct 7, 2025
43 checks passed
@zerbina zerbina deleted the earlier-top-level-emit-handling branch October 10, 2025 20:26
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 language-design Language design syntax, semantics, types, statics and dynamics. refactor Implementation refactor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants