X3J11: Why no labelled loops?

franka at mntgfx.UUCP franka at mntgfx.UUCP
Sat Feb 28 05:17:01 AEST 1987


In article <388 at cognos.UUCP> brianc at cognos.UUCP (Brian Campbell) writes:
>
>   Why are labelled loops not supported in ANSI C?
>   Does anyone have some more information on why such a useful feature
>(in my opinion) would be omitted?
>
>   - Brian Campbell

Probably because there IS a workaround for this feature which is seldom
used.

Now for my pet peeve.  Why can't you take the address of a label?  This
would be useful in writing threaded code interpreters and in doing high
performance code.  As far as I know, there is no work around for this
problem.  E.g.,

    label   *jump_table[] = { &exe_seg_0, &exe_seg_1, ... };

    while (!halt_flag) {
        /* do something to load the byte code */
        goto *jump_table[byte_code];

exe_seg_0:
        /* stuff */
        continue;

exe_seg_1:
        /* stuff */
        continue;
    }

I know that some of you will be saying how is this different than a switch
statement?  It is different in three ways

	1) A switch statement will often be converted to a series of if-then-else
    on many machines.  With this method, I have control over how the code is
    executed.  I also control range checking.

    2) I can convert the byte codes for an interpreted language into addresses
    of the actual routines at load time (can you say direct threaded code, boys
    and girls?), so that the array lookup of the above is unnecessary.  This also
    avoids the routine call overhead which is the closest way to do this in C
    currently.

    3)  It allows me to dynamically construct code in an array an execute it
    without resorting to assembly code linkages (this is very important for
    doing incremental compiling / bytecode conversion easily).

This construct is not difficult to implement on linkers, it is not any more
hazardous than the goto statement itself, and it introduces a capability into
the language which is not currently there and for which no fast workaround
exists.  In fact, I wonder why this idea has not been implemented before...

Frank Adrian
Mentor Graphics, Inc.

These opinions are my own.  Don't blame my employer.



More information about the Comp.lang.c mailing list