Is old style compile-time foreach redundant?

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Jan 9 19:31:16 UTC 2018


On Tue, Jan 09, 2018 at 02:24:11PM -0500, Steven Schveighoffer via Digitalmars-d-learn wrote:
[...]
> A break or continue is simply a goto underneath. A goto in an unrolled
> loop isn't much different than a goto in a um... rolled loop :) It's
> just that there are copies of each loop body, and the gotos need
> copies of the labels.
> 
> So no, it's not "interpreted" by the foreach statement, but the
> foreach statement provides the anchors for the goto label targets.

True.


[...]
> And then of course, the optimizer weeds out the unreachable
> statements.  Doing this with static foreach wouldn't be as pleasant.
> You'd have to branch the entire loop body, or use a goto in the case
> of a break.
[...]

If there were a hypothetical `static continue` or `static break` that's
recognized by the static foreach unroller, we could in theory automate
this branching in the compiler itself, e.g., by deleting the AST nodes
that would be skipped.  Of course, the syntax need not be `static
continue`; if there were a way to overload `break LABEL;` for the same
purpose, i.e., have the static foreach unroller inspect the label to see
if it is referring to the static foreach itself, then this would work.

But I don't know the static foreach implementation enough to be able to
tell whether this is actually possible at the time static foreach is
processed, or whether there may be some chicken-and-egg problem with
inspecting the target of a break/continue before semantic or whatever.


T

-- 
Государство делает вид, что платит нам зарплату, а мы делаем вид, что работаем.


More information about the Digitalmars-d-learn mailing list