92
Section 2: Compiler
TI
-
89 / TI
-
92 Plus Sierra C Assembler Reference Manual
Not for Distribution
Beta Version February 2, 2001
a function can be assigned to the same register if they all have nonoverlapping
lifetimes. Different lifetimes of the same variable can be allocated to different
registers or a combination of registers and stack locations.
2.15.2. Switch
Statements
The compiler uses three different code generation algorithms to implement the C
language switch statement. The algorithm employed for a given switch
statement depends on the number of case statements and the range of case
statement values.
If there are four or fewer case labels, the compiler generates a sequence of
explicit tests against the case values. This is equivalent to the code generated by
a series of if-else statements.
If there are more than four case labels, the switch algorithm selected is
determined by both the number of case statements and their values. If the case
value density is sufficiently high, a jump table is generated; otherwise, the
compiler generates code that performs an in-line binary search to locate the
appropriate case statement.
A jump table is generated when at least one third of the possible integral values
over the case value range are represented by case statements (i.e., when the
difference between the highest and lowest case values divided by the total
number of case statements is less than three). For example, a jump table would
not be generated for the following switch statement:
int i;
f()
{
switch(i) {
case 4: ... ;
case 8: ... ;
case 12: ... ;
case 16: ... ;
case 20: ... ;
default: ... ;
}
}
In this example, the binary search algorithm is used because the case density is
not high enough to justify a jump table. The difference between the maximum
and minimum case values divided by the number of case statements is not less
than three (i.e., (20 - 4) / 5 > 3).