Branch table
In computer programming, a branch table or jump table is a method of transferring program control to another part of a program using a table of branch or jump instructions. It is a form of multiway branch. The branch table construction is commonly used when programming in assembly language but may also be generated by compilers, especially when implementing optimized switch statements whose values are densely packed together.
Typical implementation
A branch table consists of a serial list of unconditional branch instructions that is branched into using an offset created by multiplying a sequential index by the instruction length. It relies on the fact that machine code instructions for branching have a fixed length and can be executed extremely efficiently by most hardware, and is most useful when dealing with raw data values that may be easily converted to sequential index values. Given such data, a branch table can be extremely efficient. It usually consists of the following 3 steps:- optionally validating the input data to ensure it is acceptable. Also, if there is no doubt about the values of the input, this step can be omitted.
- transform the data into an offset into the branch table. This usually involves multiplying or shifting it to take into account the instruction length. If a static translate table is used, this multiplying can be performed manually or by the compiler, without any run time cost.
- branching to an address made up of the base address of the branch table plus the just generated offset. This sometimes involves an addition of the offset onto the program counter register. This final address usually points to one of a sequence of unconditional branch instructions, or the instruction immediately beyond them.
... validate x /* transform x to 0 */
y = x * 4; /* multiply by branch instruction length */
goto next + y; /* branch into 'table' of branch instructions */
/* start of branch table */
next: goto codebad; /* x= 0 */
goto codeone; /* x= 1 */
goto codetwo; /* x= 2 */
... rest of branch table
codebad: /* deal with invalid input */
Alternative implementation using addresses
Another method of implementing a branch table is with an array of pointers from which the required function's address is retrieved. This method is also more recently known under such different names as "dispatch table" or "virtual method table" but essentially performing exactly the same purpose. This pointer function method can result in saving one machine instruction, and avoids the indirect jump.The resulting list of pointers to functions is almost identical to direct threaded code, and is conceptually similar to a control table.
The actual method used to implement a branch table is usually based on:
- the architecture of the processor on which the code is to be executed,
- whether it is a compiled or interpreted language and
- whether late binding is involved or not.
History
- embedded programming
- operating system development. In many operating systems, both system calls and library functions may be referenced by an integer index into a branch table.
- some computer architectures such as IBM/360 use branch tables for dispatching interrupts
Advantages
- compact code structure
- reduced source statements
- reduced requirement to test return codes individually
- Algorithmic and code efficiency, and the potential to attain high data compression ratios. For example, when compressing country names to country codes, a string such as "Central African Republic" can be compressed to a single index, resulting in large savings – particularly when the string appears many times. In addition, this same index can be used to access related data in separate tables, reducing storage requirements further.
- improve compatibility with subsequent software versions. If the code of a function and the address of its entry point is changed, only the branch instruction in the branch table needs to be adjusted; application software compiled against the library, or for the operating system, does not need modification.
Disadvantages
- Extra level of indirection
- Restrictions in some programming languages.
Example
movf INDEX,W ; Move the index value into the W register from memory
addwf PCL,F ; add it to the program counter. Each PIC instruction is one byte
; so there is no need to perform any multiplication.
; Most architectures will transform the index in some way before
; adding it to the program counter.
table ; The branch table begins here with this label
goto index_zero ; each of these goto instructions is an unconditional branch
goto index_one ; of code.
goto index_two
goto index_three
index_zero
; Code is added here to perform whatever action is required when INDEX = zero
return
index_one
...
Note: this code will work only if PCL <. To ensure this condition we may use an "org" directive. And if GOTO is 2 bytes, this limits the number of table entries to less than 128.
Jump table example in C
Another simple example, this time demonstrating a jump table rather than a mere branch table. This allows program blocks outside of the currently active procedure/function to be called:- include
- include
/* The functions */
void func3
void func2
void func1
void func0
Handler jump_table = ;
int main
Jump table example in PL/I
implements a jump table as an array of label variables. These may be initialized in an unusual way by using a subscripted statement label. PL/I label variables are not simply the address of the statement, but usually contain additional information on the state of the code block to which they belong. Without the unusual initialization, this could also be coded with calls and an array of entry variables.
declare lab label;
declare x fixed binary;
goto lab;
lab: /* code for choice 1 */ ;
...
lab: /* code for choice 2 */ ;
...
Compiler generated branch tables
Programmers frequently leave the decision of whether or not to create a branch table to the compiler, believing that it is perfectly capable of making the correct choice from the known search keys. This may be true for optimizing compilers for relatively simple cases where the range of search keys is limited. However, compilers are not as intelligent as humans and cannot have a deep knowledge of 'context', believing that a range of possible search key integer values such as 1, 2, 4, 6, 7, 20, 23, 40, 42, 50 & 1000 would generate a branch table with an excessively large number of empty entries for very little advantage. In fact, the application may be highly "time critical" and memory requirement may not really be an issue at all.However, a little 'common sense' can transform this particular case, and many other similar cases, to a simple two-step process with very large potential savings – while still eventually leaving the ultimate choice to the compiler – but 'assisting its decision' considerably:
- First, test for search key=1000 and perform appropriate branch.
- Allow the compiler to 'choose' to generate a branch table on the remaining search keys.
Computed GoTo
While the technique is now known as 'branch tables', early compiler users called the implementation 'computed GoTo', referring to the instruction found in the Fortran series of compilers. The instruction was eventually deprecated in Fortran 90.Creating the index for the branch table
Where there is no obvious integer value available for a branch table it can nevertheless be created from a search key by some form of arithmetic transformation, or could simply be the row number of a database or the entry number in an array containing the search key found during earlier validation of the key.A hash table may be required to form the index in some cases. However, for single byte input values such as A-Z, the contents of the byte itself can be used in a two-step, "trivial hash function", process to obtain a final index for a branch table with zero gaps.
- Convert the raw data character to its numeric equivalent
- Use the numeric integer value as index into a 256 byte array, to obtain a second index