C语言 --- switch语句的原理(goto版本)

阅读之前:

  • 本文copy自 《Computer Systems A Programmer’s Perspective》 ,第三版。的 3.6.8 节 switch语句。如果汇编代码部分看不懂,需要把前面的部分全部读懂。

C语言的switch的代码:

void switch_eg(long x, long n, long *dest)
{
    long val = x;
    switch (n)
    {
    case 100:
        val *= 13;
        break;
    case 102:
        val += 10;
    /* Fall through */
    case 103:
        val += 11;
        break;
    case 104:
    case 106:
        val *= val;
        break;
    default:
        val = 0;
    }
    *dest = val;
}

C语言—将switch语句翻译为扩展的C语言:

  • 跳转表(jump table):Not only do they make the C code more readable, but they also allow an efficient implementation using a data structure called a jump table. A jump table is an array where entryi is the address of a code segment implementing the action the program should take when the switch index equals i. The code performs an array reference into the jump table using the switch index to determine the target for a jump instruction.
  • &在C语言里是取址符(创建一个指向数据的指针),GCC的作者创造了一个新的操作符 &&(创建一个指向代码所在位置的指针)
void switch_eg_impl(long x, long n, long *dest)
{
    /* Table of code pointers */
    static void *jt[7] = {
        &&loc_A, &&loc_def, &&loc_B,
        &&loc_C, &&loc_D, &&loc_def,
        &&loc_D};
    unsigned long index = n - 100;
    long val;

    if (index > 6)
        goto loc_def;
    /* Multiway branch */
    goto *jt[index];

loc_A: /* Case 100 */
    val = x * 13;
    goto done;
loc_B: /* Case 102 */
    x = x + 10;
/* Fall through */
loc_C: /* Case 103 */
    val = x + 11;
    goto done;
loc_D: /* Cases 104, 106 */
    val = x * x;
    goto done;
loc_def: /* Default case */
    val = 0;
done:
    *dest = val;
}

上面代码用GCC进行运行的结果如图:

C语言 --- switch语句的原理(goto版本)

翻译为汇编代码(Part 1):

  • jmp指令前的操作数,带了一个 *号,表示一个非直接跳转。并且该操作数指的是一个存储器的位置,地址的值在%eax里(and the operand specifies a memory location indexed by register %eax, which holds the value of index )。

注:jmp的地址 8*%rsi + .L4

		void switch_eg(long x, long n, long *dest)
		x in %rdi, n in %rsi, dest in %rdx
1 	switch_eg:
2       subq  $100, %rsi               Compute index = n-100
3       cmpq  $6, %rsi                Compare index:6
4       ja    .L8                     If >, goto loc_def
5       jmp   *.L4(,%rsi,8)           Goto *jg[index]
6     .L3:                             loc_A:
7       leaq (%rdi,%rdi,2), %rax        3*x
8       leaq  (%rdi,%rax,4), %rdi      val = 13*x
9       jmp   .L2                        Goto done
10    .L5:                             loc_B:
11      addq  $10, %rdi               x = x + 10
12    .L6:                            loc_C:
13      addq  $11, %rdi               val = x + 11
14      jmp  .L2                       Goto done
15    .L7:                            loc_D:
16      imulq %rdi, %rdi              val = x * x
17      jmp .L2                      Goto done
18    .L8:                           loc_def:
19      movl $0, %edi                 val = 0
20    .L2:                          done:
21      movq %rdi, (%rdx)            *dest = val
22      ret                          Return

汇编代码跳转表部分(Part 2):

  • .rodata (表示 “read-only data”)
  • 有着连续的7个quad(8字节)words,每个word的值都是声明了的指令的地址(比如:.L3)。.L4标记了定位的开始,该标签的地址是非直接跳转的基地址(part 1的第五行)。(there should be a sequence of seven “quad” (8- byte) words, where the value of each word is given by the instruction address associated with the indicated assembly-code labels (e.g., .L3). Label .L4 marks the start of this allocation. The address associated with this label serves as the base for the indirect jump (line 5).)
  • 可以看到:在有很多个分支的情况下,使用跳转表是很高效的,可以一步到位。
1 	.section 	.rodata
2 	.align 			8 Align address to multiple of 8
3  .L4:
4 	.quad .L3 		Case 100: loc_A
5 	.quad .L8 		Case 101: loc_def
6 	.quad .L5 		Case 102: loc_B
7 	.quad .L6 		Case 103: loc_C
8 	.quad .L7 		Case 104: loc_D
9 	.quad .L8 		Case 105: loc_def
10 	.quad .L7 		Case 106: loc_D
上一篇:算法计算时间复杂度(1):求递归式 f(n) = 2f(n/2) + n


下一篇:漏刻有时多功能php-API接口开发实例