CompilerDev:实现条件语句与循环

来源:OSDev Wiki· 阅读原文 ↗

一般概念

对于像 x86、ARM、8051、MIPS 或目前广泛使用的大多数其他 CPU 类型(2016)这样的传统指令集架构,条件语句如 if/elsif/else,以及循环结构如 for 或 while,通常通过测试、条件跳转/分支(jz、beq 等)和无条件分支(jmp、bra、j 等)的组合来实现。虽然一些常见 ISA 具有用于重复或循环的专用指令(例如 x86 上的 REP 和 LOOP 指令),但很少有编译器使用它们,因为确定它们可以应用在何处会增加额外的工作。

下面给出的示例针对 Intel x86-64、 ARM、MIPS R2000 汇编语言 指令集。假设编译器生成汇编语言;对于直接生成 可执行文件 的编译器,编译器必须处理原本由汇编器完成的内务工作(例如跟踪分支目标)。

一般条件语句

If

基本 if 语句的朴素实现通常包括:当条件为真时跳转到代码的条件分支(即 结果部分),跳过结果部分的无条件分支,以及结果部分本身:

if (a == b)
{
    /* do something */
}

生成的代码(假设 a 和 b 已经位于相应的寄存器中)可能如下所示:

x86-64 ARM MIPS
    cmp rax, rbx
    je main.if0.true
    jmp main.if0.end
main.if0.true:
    ;;; consequent
main.if0.end:
    teq r0, r1
    beq main.if0.true
    b main.if0.end
main.if0.true:
    ;;; consequent
main.if0.end:    
    beq $t0, $t1, main.if0.true
    j main.if0.end
main.if0.true:
    ### consequent
main.if0.end:

注意,编译器必须为每个分支目标生成唯一的标签或目标表条目;最简单的实现方式是维护一个标签计数,并为它们分配一个附加了计数的独立标签名。为了可读性,示例代码使用了带有函数名(本例中为 main)、表达式类型以及该类型表达式计数的局部标签。

因此,对于嵌套的 if,例如

if (a == b)
{
    if (b <= c)
    {
        /* do the inner clause */
    }
    /* do the rest of the outer clause */
}


它可能产生:

x86-64 ARM MIPS
    cmp rax, rbx
    je main.if1.true
    jmp main.if1.end
main.if1.true:
    cmp rbx, rcx
    jle main.if2.true
    jmp main.if2.end
main.if2.true:
    ;;; do the inner clause
main.if2.end:
    ;;; do the rest of the outer clause
main.if1.end:

    bge $t0, $t1, main.if1.true
    j main.if1.end
main.if2.true:
    blt $t0, $t2, main.if2.true
    j main.if2.end
main.if2.true:
    ;;; consequent for inner conditional
main.if2.end:
    ;;; remaining code
main.if1.end:
复合布尔条件

对于复合条件,例如逻辑 AND 或逻辑 OR,朴素实现是先执行测试,然后使用逻辑运算产生可测试的结果。

if (!(a <= b && b < c))
    /* do something */
}

它可能朴素地生成

x86-64 ARM MIPS


    ble $t0, $t1, main.if3.and0.left.true
    clear $t3
    j main.if3.and0.right
main.if3.and0.left.true:
    move $t3, 1
    j main.if3.and0.right
main.if3.and0.right:
    ble $t0, $t1, main.if3.and0.right.true
    clear $t3
    j main.if3.and0.test
main.if3.and0.right.true:
    move $t3, 1    
    j main.if3.and0.test
main.if3.and0.test:    
    and $t3, $t3, $t4
    not $t5, $t3
    bnez $t5, main.if3.true
    j main.if3.end
main.if3.true:
    ;;; consequent
main.if3.end:


虽然这是对条件的字面翻译,但显然不是最优的。

基本优化

对此一个明显且相当简单的优化是取反或反转条件,从而消除对无条件分支的需要:

x86-64 ARM MIPS
    cmp rax, rbx
    jne main.if0.end
    ;;; consequent
main.if0.end:
    teq r0, r1
    bne main.if0.end
    ;;; consequent
main.if0.end:    
    bne $t0, $t1, main.if4.end
    ;;; consequent
main.if4.end:

类似地,对于嵌套的 if,当内层 if 是外层循环的第一个或最后一个子句时,它可以移除一个多余的标签:

x86-64 ARM MIPS
    cmp rax, rbx
    jne main.if1.end
    ;;; if(rax == rbx)
      cmp rbx, rcx
      jg main.if2.end
      ;;; if(rbx <= rcx)
      ;;; do the inner clause
main.if2.end:
    ;;; do the rest of the outer clause
main.if1.end:

    blt $t0, $t1, main.if5.end
    bge $t0, $t2, main.if6.end
    ;;; inner consequent
main.if6.end:
    ;;; remaining code of outer consequent
main.if2.end:


对于这些简单情况,由于给定运算符到其逆运算的映射是固定的(例如,要实现“小于”,你总是替换为“大于或等于”),唯一需要的更改是测试代码按逆运算发出。

优化复合条件要棘手一些,因为你需要考虑哪些运算会反转哪些其他运算,以及如何应用诸如德摩根定律之类的东西。然而,对于大多数编译器来说,执行某些消除仍然是合理的,例如用短路分支替换单独的测试。在 AND' 的情况下,可以从将第一个测试短路到 and 部分条件的末尾开始,这样只有在第一个为真时才检查第二个测试。对于 NOT,你只需对最后一种情况不取反即可得到正确结果:

然而,如果付出更多努力,可能会得到更好的解决方案,即维护一个布尔函数及其逆函数的表,并愿意对运算顺序进行简单分析。

确定性循环

确定性循环的朴素实现是在循环开始处放置条件分支,并在循环结束处放置跳回该条件分支的无条件分支,这也是显式 for 循环的朴素实现。MIPS 汇编代码中的示例(使用伪指令)可能如下:

x86-64 ARM MIPS
        xor rcx, rcx
        mov rbx, for_loop_count
for0.start:
        ; if rcx is greater than or equal 
        ; to rbx, jump past the loop
        cmp rcx, rbx
        jge for1.end
        ;;  body of the loop here
        inc rcx
        jmp for0.start
for0.end:
        mov r0, #0                 
        mov r1, #for_loop_count    
for0.start:
        ; if r1 is greater than or equal 
        ; to r0, jump past the loop     
        cmp r0, r1
        bge for1.end       
        ;;  body of the loop here
        add r0, r0, #1
        b for0.start
for0.end:
        clear $t0                 
        move $t1, for_loop_count    
for0.start:
        ; if t1 is greater than or equal 
        ; to t0, jump past the loop     
        bge $t0, $t1, for1.end        
        ;;  body of the loop here
        addi $t0, $t0, 1
        bra for0.start
for0.end

然而,即使是朴素的编译器通常也会通过使用无条件分支,后跟循环标签,再后跟循环体,然后将循环条件(取反后)作为原始无条件分支的目标,来进行 循环反转:

x86-64 ARM MIPS


   clear $t0
        move $t1, for_loop_count    
        bra for1.test
for1.start:
        ;;  body of the loop here
        addi $t0, $t0, 1
for1.test:
        ; if t1 is less than to t0, 
        ; jump past the loop
        blt $t0, $t1, for1.start         
for1.end

这意味着在进入循环后,循环中只剩下无条件分支,从而使其更快。对于手工编写的递归下降编译器,这只需更改解析-发射函数的调用顺序即可。

来源与翻译说明

本文根据原文快照翻译,保留原有代码、数值、技术限定及历史背景。原文中的命令与运行结果不代表本站已实际执行验证。

原文:CompilerDev/Implementing Conditional Statements And Loops;离线修订号:28080。对应原文版本

原作者:OSDev Wiki 社区贡献者。原文及引用资料的版权与许可声明继续适用:版权声明 许可说明

评论

暂无评论