Next: 8.1.8 Bit-shifting Operations.
Up: 8.1 Optimizations
Previous: 8.1.6 Algebraic Simplifications
Contents
Index
8.1.7 'switch' Statements
SDCC changes switch statements to jump tables
when the following conditions are true.
- The case labels are in numerical sequence, the labels need not be
in order, and the starting number need not be one or zero.
switch(i) { switch
(i) {
case 4: ... case
0: ...
case 5: ... case
1: ...
case 3: ... case
2: ...
case 6: ... case
3: ...
} }
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.
- The number of case labels is at least three, since it takes two conditional
statements to handle the boundary conditions.
- The number of case labels is less than 84, since each label takes
3 bytes and a jump-table can be utmost 256 bytes long.
Switch statements which have gaps in the numeric sequence or those
that have more that 84 case labels can be split into more than one
switch statement for efficient code generation, e.g.:
switch (i) {
case 1: ...
case 2: ...
case 3: ...
case 4: ...
case 9: ...
case 10: ...
case 11: ...
case 12: ...
}
If the above switch statement is broken down into two switch statements
switch (i) {
case 1: ...
case 2: ...
case 3: ...
case 4: ...
}
and
switch (i) {
case 9: ...
case 10: ...
case 11: ...
case 12: ...
}
then both the switch statements will be implemented using jump-tables
whereas the unmodified switch statement will not be. You might also
consider inserting dummy cases 0 and 5 to 8 in this example.
The pragma nojtbound can be used to turn
off checking the jump table boundaries. It has
no effect if a default label is supplied. Use of this pragma is dangerous:
if the switch argument is not matched by a case statement the processor
will happily jump into Nirvana.
Next: 8.1.8 Bit-shifting Operations.
Up: 8.1 Optimizations
Previous: 8.1.6 Algebraic Simplifications
Contents
Index
Bernhard Held
2004-02-21