简体   繁体   English

切换执行; 臂; 汇编器 aarch64; 臂64

[英]switch implementation; arm; assembler; aarch64; arm64

I'm interesting in the ways to implement 'switch' operator on aarch64 assembler. 我对在aarch64汇编器上实现“ switch”运算符的方式很感兴趣。 On arm32 platforms I used something like 在arm32平台上,我使用了类似

    ldr         pc,         [pc, ta, LSL#2]
    nop                                         // alignment
    .int        .L.case1
    .int        .L.case2
    ...
    .int        .L.caseN

But since 64bit version has a lot of restriction on 'pc' register usage, such implementation doesn't work any more. 但是由于64位版本对“ pc”寄存器的使用有很多限制,因此这种实现不再起作用。

It seems that the easiest way is to use pair of compare and branch operations, like 似乎最简单的方法是使用一对比较和分支操作,例如

cmp ta, #1
b.eq .L.case1
cmp ta, #2
b.eq .L.case2
...

But sometimes there are up to dozen cases and it would lead to a significant delay before reaching the last 'case'. 但是有时最多有十几个案件,这将导致在到达最后一个“案件”之前的重大延迟。

Could you share your ideas how to implement fast switch on aarch64, please. 请您分享一下如何在aarch64上实现快速切换的想法。

Thanks :) 谢谢 :)

I don't have an 64-bit ARM assembler to test this with but I believe you would do something like the following to implement a jump table: 我没有用于测试的64位ARM汇编器,但我相信您会执行以下操作来实现跳转表:

    adr  x0, jmp_table
    ldr  x0, [x0, x1, LSL#3]
    br   x0

jmp_table:
    .quad .L.case1
    .quad .L.case2
    .quad .L.case3

The first instruction, ADR , loads the address of a label into a register. 第一条指令ADR将标签的地址加载到寄存器中。 The last instruction, BR , jumps to the address stored in the register. 最后一条指令BR跳转到寄存器中存储的地址。

If you're creating a shared library or a position independent executable you can try something like following: 如果要创建共享库或与位置无关的可执行文件,则可以尝试以下操作:

    adr  x0, jmp_table
    add  x0, x0, x1, LSL#2
    br   x0

jmp_table:
    b .L.case1
    b .L.case2
    b .L.case3

Alternate PIC example 备用PIC示例

    adr  x0, jmp_table
    ldr  w1, [x0, x1, LSL#2]
    add  x0, x0, x1
    br   x0

jmp_table:
    .int  .L.case1 - jmp_table
    .int  .L.case2 - jmp_table
    .int  .L.case3 - jmp_table

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM