Thursday, November 4, 2010

Jump Table - An optimization Technique

        
         Consider a C program which contain a switch statement with 4 cases. Each case contain consecutive integers. When compiling the compiler will go through the complete code and manages to keep a count of similar codes and consecutive values. If this count is greater than a limit(say 5) then the compiler will try to simplify the code by using some optimization techniques.
        Jump table is one of the optimization technique used by the compiler. Jump Table transfers the program control from one point to another based on the branch instruction table. If the count is less than the limit then the compiler will generate assembly for each code.
       Consider a C program which contain switch statement with 15 cases. Now the compiler will go through the whole code and will generate a jump table. 
       Now we can consider the assembly code.

        Here the value to be checked is first moved to stack (r4 contain the address of stack). If this value is beyond the limit of switch statement then exit else save the value in the stack to a register. Then rotate left(rla)the value in the register and add with the address of the jump table(#.L18). By rotating and adding we will get the corresponding address of the label from the jump table. Then moving the value in the address specified in the register to another register(say r6) and calling branch of this register will jump to the corresponding label.

        The above figure is the jump table which contain all the address of the labels. The addresses of labels are saved in the consecutive memory location starting from the memory location of the address of .L18.
        If the register contain the address of .L11 then the control will go to .L11
and replaces the value to the stack and exits.

No comments:

Post a Comment