mirgen: improve scope-only block
support
#928
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Don't emit
mnkBlock
MIR nodes fornkBlockStmt
/nkBlockExpr
nodes(
block
statements/expression) are not used for control-flow. Thisreduces the size of the produced MIR code and means that neither
compiler-generated scope-only blocks nor
block
statements/expression not targeted by
break
statements reach the codegenerators.
Most notably, the code generated by the JavaScript code generator
improves, in terms of size.
Details
Detection of blocks-used-for-control-flow is done via testing for the
sfUsed
flag: if present, thenkBlockStmt
/nkBlockExpr
is treatedas used for control-flow, otherwise it's treated as only opening a
scope. This is only a conservative approximation, however, and blocks
where the label is marked as used through other means (or by appearing
in a
break
within acompiles
) will be mis-detected.So that the detection also works for
transf
-injected breaks andblocks, all
nkBreakStmt
AST intransf
/closureiters
/lambdalifting
is now generated via the new
newBreakStmt
procedure inlowerings
,which marks the provided label as used.
When
mirgen.genBlock
gets passed annkBlockExpr
/nkBlockStmt
nodewhere the label symbol is not marked as used, it only emits the
mnkScope
block necessary for correct variable lifetimes. SincecnkBlockStmt
are only generated formnkBlock
nodes bycgirgen
,this means that the block statements are omitted in the final generated
code too.
Finally,
transf.transformBlock
is improved, and a necessary fix isapplied:
newLabel
during inlining,the original symbol is copied and adjusted. This ensures that the
symbol flags (e.g., whether the symbol is used) are kept
breakSyms
stack in theinlining case. This was/is unnecessary, as inlined AST is already
transformed, and so none of the
nkBreakStmt
nodes within areprocessed
transformSons
, but transformsthe body AST directly. While having no practical effect at the
moment, this means that the label slot is no longer treated as a
symbol-in-a-usage-position
No
mnkBlock
nodes being emitted for scope-only blocks is also visiblein the
--expandArc
output, so the tests depending on the output areadjusted.