On 1/19/2013 9:05 PM, William Ahern wrote:
> BGB <> wrote:
> <snip>
>> some C extensions, such as computed goto, can help to a limited extent
>> here, but given computed goto can't cross function boundaries (or, by
>> extension, boundaries between one compilation unit and another), a limit
>> is placed on its use here (IOW: it doesn't scale very well).
> <snip>
>> an alternative compromise could be though if (as a C extension) it were
>> possible to fetch and call label-pointers as if they were function
>> pointers (using the same signature as that of the parent function), and
>> also potentially refer to them from outside the function in question
>> (more like a struct), but currently I am not aware of any compiler which
>> supports this.
>
>> foo.label1(3, 4); //call into foo() via an arbitrary label.
>> fcn=&foo.label1; //get label pointer as function pointer.
>
>> possibly with something inside foo like:
>> "extern label1:"
>> with extern giving a compiler hint that maybe it should generate any
>> glue needed to make this callable.
> <snip>
>
> I was surprised to find that the following GCC hack works:
>
> #include <stdio.h>
>
> static void *One, *Two;
>
> static int foo(void *label) {
> __label__ one, two;
> __attribute__((constructor, used)) void bar(void) {
> One = &&one;
> Two = &&two;
> }
>
> goto *label;
> one:
> return 1;
> two:
> return 2;
> } /* foo() */
>
> int main(void) {
> printf("one: %d\n", foo(One));
> printf("two: %d\n", foo(Two));
>
> return 0;
> } /* main() */
>
> I discuss some of the details in a blog post "Acquiring address of labels
> outside of scope": http://25thandclement.com/~william/blog/
>
well, yes, it works...
the downside is, of course, that you still have to call into the
function (or otherwise be inside the the function) to goto the label
(and have a single massive function to make much use of it).
I am imagining if some of these restrictions were lifted, such that a
language would allow direct inter-procedural goto, and ability to call
directly to labels within a function, so in these regards would be more
like assembler (sort of like a macro-assembler with more C like syntax).
I started writing out some ideas for such a language, but it isn't
really C (the idea ended up involving largely dropping
structured-programming constructs, instead treating functions more as a
way of declaring the active stack-frame layout), ... and would probably
use a simplistic single-pass compiler, and be largely untyped.
(in contrast, C puts considerably more distance between the language and
the underlying ASM code).
still thinking on it though, don't yet know if I will do anything with
the idea.
or such...