one pass switch code (was forward references in typedefs)

Chris Torek chris at mimsy.UUCP
Wed Jul 26 07:03:58 AEST 1989


>>In article <24CB9E07.9547 at marob.masa.com> cowan at marob.masa.com (John Cowan)
wrote:
>>>Ufcawss, if you talk to the people who wrote VMS C, they'll tell you that
>>>all one-pass implementations of C are unacceptable!  (This has something to
>>>do with generating good code for the 'switch' statement.)

>In article <18729 at mimsy.UUCP> I (Chris Torek) answered:
>>They are both right and wrong.  One-pass code generation means there
>>are few opportunities for optimsiation; but generating good switches
>>is easy.  Simply emit a branch at the top of the switch, code at each
>>of the labels, a branch at the bottom, and then generate the code
>>that actually implements the switch.  E.g., given
>>
>> [Clever example deleted]

Now in article <686 at ftp.COM> wjr at ftp.COM (Bill Rust) writes:
>... two ways to implement switches: jump tables and compare and execute
>when equal.  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.

So far so good.

>This is very difficult to do in a one pass compiler.

Competely wrong.  See text above and example in <18729 at mimsy.UUCP>,
and see below.

>The previous correspondent completely ignored this in his response.

False.

``Simply emit a branch at the top of the switch, code at each of the
  labels, a branch at the bottom, and then generate the code that
  actually implements the switch.''

Since the code that implements the switch is not generated until after
all the cases are known, the compiler can select one of the various
alternatives.  (Incidentally, heap switches are better for sparse
tables above a moderate number of comparisons.  PCC has used all three
of these forms---direct, heap, and jump table switches---for years, and
PCC is a one-pass compiler.)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list