Arcane C hacks?
herndon at umn-cs.UUCP
herndon at umn-cs.UUCP
Tue Feb 18 14:44:00 AEST 1986
I wrote a mild flame in another newsgroup about this, but
somebody out there might know another way.
The problem is this: I'd like to construct a jump table by
putting lots of labels into an array, and then issuing a
statement like
goto jumptab[i];
Unfortunately, labels are no longer simple integers (and haven't
been for most of a decade now). They can no longer be type coerced
to integer or pointer-to-integer, nor can other types be coerced
to type label. Is there any way around this? Admittedly, for
the static case with a dense list, a switch statement usually
constructs a jump table, but I'd like to construct one on the fly.
If I just want to execute machine code out of an array ("goto
array_name;" used to be legal C) I can write a simple assembly
language routine of one argument to do it. Is there a legal way
to do this in pure C any more? (Yes, one can type coerce the
address of the array to pointer-to-function and call it, but
is it possible to jump to the array?)
Both of these problems are admittedly things one would not often
do. They are, however, things one might do in an interpreter.
The former operation may be considered to be "machine independent",
as there is one reasonable, consistent interpretation. The latter
is much more open to question, as the contents of the array are not
machine independent. However, if one is to write portable inter-
preters, being able to jump to an array without assembly language
help would be a plus. (Individual pseudo-machine operations can
then be relegated to data tables, leaving the C code machine
independent.)
Robert Herndon
...!{ihnp4|stolaf}!umn-cs!herndon
More information about the Comp.lang.c
mailing list