Related articles |
---|
methods used by compilers for switch{ case } statements? mathog@seqaxp.bio.caltech.edu (2000-03-03) |
Re: methods used by compilers for switch{ case } statements? ast@halcyon.com (Andrew Tucker) (2000-03-06) |
Re: methods used by compilers for switch{ case } statements? rhyde@shoe-size.com (Randall Hyde) (2000-03-06) |
Re: methods used by compilers for switch{ case } statements? lehotsky@tiac.net (Alan Lehotsky) (2000-03-06) |
From: | Alan Lehotsky <lehotsky@tiac.net> |
Newsgroups: | comp.compilers |
Date: | 6 Mar 2000 01:12:30 -0500 |
Organization: | Quality Software Management |
References: | 00-03-006 |
Keywords: | code |
mathog@seqaxp.bio.caltech.edu wrote:
>>>Can someone please point me to an online reference which describes the
>>>mechanism(s) compilers use to implement the switch/case statement in
>>>C? Specifically, which method or methods does the object code
>>>implement?
You've asked an under-constrained question. Primarily because
the mechanisms are highly dependent on the target machine.
Further, it is also very dependent on things like the
density of cases (e.g. a reasonably contiguous range of values)
For example, on a VAX, there was a CASE instruction that took
an index value, an offset and a table size (N), followed by N
PC-relative offsets.
Most reasonable compilers try to calculate some sort of time-space
tradeoff between using a dispatch table and generating if-then-else.
Some compilers build hash functions between the index values and
the jump-addresses. It all depends on how much effort you're
willing to invest in your compiler....
ANY tradeoff discussions as to which technique to apply have to
be made in the context of a target architecture.
-- Al Lehotsky
--
Quality Software Management
http://www.tiac.net/users/lehotsky
Return to the
comp.compilers page.
Search the
comp.compilers archives again.