Is old style compile-time foreach redundant?
Steven Schveighoffer
schveiguy at yahoo.com
Tue Jan 9 20:26:32 UTC 2018
On 1/9/18 2:31 PM, H. S. Teoh wrote:
> 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.
Yeah, I think in terms of static foreach, it's not a straightforward
problem. Because the compiler may not know enough information at the
time to figure out whether it should still keep generating code.
For example:
static foreach(i; 0 .. 5)
{
if(i == 3) static break;
static assert(i < 3);
}
How does it know whether the static break should be "executed" at
compile-time if it hasn't evaluated the if-statement? The code would
have to have no runtime branches to make sure that static break can be
evaluated at compile-time.
And this still puts it at a disadvantage when compared to tuple-foreach,
at least as far as break/continue are concerned.
-Steve
More information about the Digitalmars-d-learn
mailing list