forward references in typedefs

David Goodenough dg at lakart.UUCP
Sat Jul 29 00:21:46 AEST 1989


wjr at ftp.COM (Bill Rust) sez:
> Excuse me. I am not a compiler writer but even I know that there are
> two ways to implement switches: jump tables and compare and execute
> when equal.

That makes three - see below, plus the hashing technique used in the V6
unix cc, on the PDP11 I used at university makes 4. Plus the concept
(when switching on a character value) of generating a string of recognised
characters, calling index, turning the pointer returned back into and index
and using _THAT_ to index into a table (which is in fact a variation on
my method #3 below) Of course, compiler purists will say that I have to
do the comparisons as integer values, but don't knock it, because it does
work.

> Jump tables (ie jmp switch_list[ix] where ix is some easy
> transformation of the switch variable) are much more efficient when
> the range of values that the switch variable can assume is limited (ie
> switch variable in range of 1 to 20 and it assumes 75% of values). The
> only way to tell if a jump table is better than compare and jump is to
> see what the range of the switch variable is and how many values it
> actually assumes. This is very difficult to do in a one pass compiler.
> The previous correspondent completely ignored this in his response.

Hold on a minute. Doing the necessary work is very _EASY_ in a one pass
compiler. Just like Chris Torek said, you jump at the top, build your
switch code with labels, creating the table of values as you go. Now, at
the bottom you have a table full of values. You can do anything you like
with it - simple index lookup (jump table[ix]), tabled compare (method 3):

	dw	label1, value1
	dw	label2, value2

with a search comparing value1 to the expression, or anything else that
takes your fancy. All in one pass.
-- 
	dg at lakart.UUCP - David Goodenough		+---+
						IHS	| +-+-+
	....... !harvard!xait!lakart!dg			+-+-+ |
AKA:	dg%lakart.uucp at xait.xerox.com		  	  +---+



More information about the Comp.lang.c mailing list