Arcane C hacks?

Barry Shein bzs at bu-cs.UUCP
Fri Feb 21 03:33:44 AEST 1986


Re: what to do now that goto jumptab[x] is gone?

Well, the obvious solution is a switch statement but that does not
fulfill all your requirements and you probably rejected that.

The way I do such things is to use an array of pointers to functions.
In your example of jumping to on-the-fly generated code I suspect that
is really what you are saying: On-the-fly generated functions, having
an environment around the generated code is certainly not a bad thing
and we assume you would like to come back sometime. Generating the
prologue and epilogue to make the generated code a function should be
more or less trivial as, besides stack offsets for autos, it's a cliche.

So, the answer would be to declare a table something like:

int (*funtable[MAXFNS])() ;	/* did I get that right? array of pointers
				   to functions returning int */

and just malloc the storage for the generated code. Obviously the return
value needn't be int. I can't think of any reason off hand why this isn't
powerful enough for what you propose. It should be quite portable (code
generator aside) and is legal C.

	-Barry Shein, Boston University



More information about the Comp.lang.c mailing list