Sophie

Sophie

distrib > Fedora > 14 > x86_64 > media > updates > by-pkgid > e677bbbdff6d27fe001f15e0ef2bb4cc > files > 161

sdcc-3.0.0-0.fc14.x86_64.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">

<!--Converted with LaTeX2HTML 2008 (1.71)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>8.1.7 'switch' Statements</TITLE>
<META NAME="description" CONTENT="8.1.7 'switch' Statements">
<META NAME="keywords" CONTENT="sdccman">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">

<META NAME="Generator" CONTENT="LaTeX2HTML v2008">
<META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css">

<LINK REL="STYLESHEET" HREF="sdccman.css">

<LINK REL="next" HREF="node177.html">
<LINK REL="previous" HREF="node175.html">
<LINK REL="up" HREF="node169.html">
<LINK REL="next" HREF="node177.html">
</HEAD>

<BODY >
<!--Navigation Panel-->
<A NAME="tex2html3455"
  HREF="node177.html">
<IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A> 
<A NAME="tex2html3449"
  HREF="node169.html">
<IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A> 
<A NAME="tex2html3443"
  HREF="node175.html">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A> 
<A NAME="tex2html3451"
  HREF="node1.html">
<IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A> 
<A NAME="tex2html3453"
  HREF="node191.html">
<IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="index.png"></A> 
<BR>
<B> Next:</B> <A NAME="tex2html3456"
  HREF="node177.html">8.1.8 Bit-shifting Operations.</A>
<B> Up:</B> <A NAME="tex2html3450"
  HREF="node169.html">8.1 Optimizations</A>
<B> Previous:</B> <A NAME="tex2html3444"
  HREF="node175.html">8.1.6 Algebraic Simplifications</A>
 &nbsp; <B>  <A NAME="tex2html3452"
  HREF="node1.html">Contents</A></B> 
 &nbsp; <B>  <A NAME="tex2html3454"
  HREF="node191.html">Index</A></B> 
<BR>
<BR>
<!--End of Navigation Panel-->

<H2><A NAME="SECTION00917000000000000000"></A><A NAME="sub:_switch_-Statements"></A><A NAME="4096"></A>
<BR>
8.1.7 'switch' Statements
</H2>

<P>
SDCC can optimize switch statements to jump tables<A NAME="4097"></A>.
It makes the decision based on an estimate of the generated code size.
SDCC is quite liberal in the requirements for jump table generation: 

<UL>
<LI>The labels need not be in order, and the starting number need not
be one or zero, the case labels are in numerical sequence or not too
many case labels are missing.

<P>
<BLOCKQUOTE>
<TT>switch(i) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch
(i) { </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;case 4: ...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case
0: ... </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;case 5: ...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb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nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case
6: ... </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;case 10: ...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case
7: ... </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;case 11: ...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case
8: ... </TT>&nbsp;
<BR><TT>}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</TT>

</BLOCKQUOTE>

<P>
Both the above switch statements will be implemented using a jump-table.
The example to the right side is slightly more efficient as the check
for the lower boundary of the jump-table is not needed.

<P>
</LI>
<LI>The number of case labels is not larger than supported by the target
architecture.
</LI>
<LI>If the case labels are not in numerical sequence ('gaps' between cases)
SDCC checks whether a jump table with additionally inserted dummy
cases is still attractive. 
</LI>
<LI>If the starting number is not zero and a check for the lower boundary
of the jump-table can thus be eliminated SDCC might insert dummy cases
0, ... .
</LI>
</UL>
Switch statements which have large gaps in the numeric sequence or
those that have too many case labels can be split into more than one
switch statement for efficient code generation, e.g.:
<BLOCKQUOTE>
<TT>switch (i) { </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;case 1: ... </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;case 2: ... </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;case 3: ... </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;case 4: ... </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;case 5: ... </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;case 6: ... </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;case 7: ... </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;case 101: ... </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;case 102: ... </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;case 103: ... </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;case 104: ... </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;case 105: ... </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;case 106: ... </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;case 107: ... </TT>&nbsp;
<BR><TT>}</TT>

</BLOCKQUOTE>
If the above switch statement is broken down into two switch statements
<BLOCKQUOTE>
<TT>switch (i) { </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;case 1: ... </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;case 2: ... </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;case 3: ... </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;case 4: ... </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;case 5: ... </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;case 6: ... </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;case 7: ... </TT>&nbsp;
<BR><TT>}</TT>

</BLOCKQUOTE>
and
<BLOCKQUOTE>
<TT>switch (i) { </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;case 101: ... </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;case 102: ... </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;case 103: ... </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;case 104: ... </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;case 105: ... </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;case 106: ... </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;case 107: ... </TT>&nbsp;
<BR><TT>}</TT>

</BLOCKQUOTE>
then both the switch statements will be implemented using jump-tables
whereas the unmodified switch statement will not be.

<P>
The pragma nojtbound<A NAME="4153"></A> can
be used to turn off checking the <I>j</I>ump <I>t</I>able <I>bound</I>aries.
It has no effect if a default label is supplied. Use of this pragma
is dangerous: if the switch<A NAME="4157"></A> argument is not
matched by a case statement the processor will happily jump into Nirvana.

<P>
<HR>
<!--Navigation Panel-->
<A NAME="tex2html3455"
  HREF="node177.html">
<IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A> 
<A NAME="tex2html3449"
  HREF="node169.html">
<IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A> 
<A NAME="tex2html3443"
  HREF="node175.html">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A> 
<A NAME="tex2html3451"
  HREF="node1.html">
<IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A> 
<A NAME="tex2html3453"
  HREF="node191.html">
<IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="index.png"></A> 
<BR>
<B> Next:</B> <A NAME="tex2html3456"
  HREF="node177.html">8.1.8 Bit-shifting Operations.</A>
<B> Up:</B> <A NAME="tex2html3450"
  HREF="node169.html">8.1 Optimizations</A>
<B> Previous:</B> <A NAME="tex2html3444"
  HREF="node175.html">8.1.6 Algebraic Simplifications</A>
 &nbsp; <B>  <A NAME="tex2html3452"
  HREF="node1.html">Contents</A></B> 
 &nbsp; <B>  <A NAME="tex2html3454"
  HREF="node191.html">Index</A></B> 
<!--End of Navigation Panel-->
<ADDRESS>

2011-03-20
</ADDRESS>
</BODY>
</HTML>