computed goto, const, truncated int, C++, etc.

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

computed goto, const, truncated int, C++, etc.

computed goto.

The documentation advertises read only relative address.​

Like this:​​

static const int array[] = { &&foo - &&foo, &&bar - &&foo,​
                             &&hack - &&foo };​
goto *(&&foo + array[i]);​

However, this doesn't quite work and is a little flawed.​

What one wants is:​

1. int or int32_t, as stated.​
1b. Or a target specific type provided by the compiler​
that encompasses the largest distance in a function.​
But in reality that is int/int32_t.​

Not blow the space unnecessarily on a 64bit integer, if executables and therefore distances within functions are limited to 32bits (I realize, there is a signnedess problem hypothethically, but ultimately I expect an assembler or linker warning for the label math overflow).

2. Syntax that works in C and C++.​
And is truly const, no dynamic initializer.
​This is crucial.

3. Preferably without casting.​
​But if I must, ok.

4. Instead of relative to a label, I should be able to use relative​
to the array itself. Which then only allows a single ampersand.​
Double might be nice, but whatever works.​
I think that might save an instruction.
It isn't critical.

5. 32bit and 64bit.​

Many combinations do work, but you sometimes have to cast​
to char* or int or size_t.​
Sometimes have to narrow.​
Only sometimes can use the address of the array.​
Not always valid C and C++.​

And not all combinations do work.​
We have code that compiles as C or C++, unless/until we decide​
to use C++, and I couldn't make it work across the board.​

But now I'll see if the code is really any better than switch...

 - Jay