On Thu, Sep 25, 2025 at 2:50 PM Jan Mercl <
0xj...@gmail.com> wrote:
> Is it possible to write code in Go ABI0 assembler that implements a jump table?
For posterity, this proof of concept seems to work:
        jnml@e5-1650:~/tmp/jumptable$ cat main.go
        package main
        // int switch_func(int op) {
        //     int result;
        //     switch (op) {
        //         case 0:
        //             result = 100;
        //             break;
        //         case 1:
        //             result = 200;
        //             break;
        //         case 2:
        //             result = 300;
        //             break;
        //         default:
        //             result = -1;
        //             break;
        //     }
        //     return result;
        // }
        func switch_func(op int64) int64
        func main() {
                for i := int64(0); i < 4; i++ {
                        println(i, switch_func(i))
                }
        }
        jnml@e5-1650:~/tmp/jumptable$ cat main.s
        #include "funcdata.h"
        #include "textflag.h"
        TEXT ·switch_func(SB), $0-16
                GO_ARGS
                NO_LOCAL_POINTERS
                JMP over
                JMP case0
                JMP case1
                JMP case2
        over:
                MOVQ op+0(FP), AX
                CMPQ AX, $2
                JA   dflt
                LEAQ ·switch_func+2(SB), CX
                ADDQ AX, AX
                ADDQ AX, CX
                JMP CX
        case0:
                MOVQ $100, AX
                JMP  done
        case1:
                MOVQ $200, AX
                JMP  done
        case2:
                MOVQ $300, AX
                JMP  done
        dflt:
                MOVQ $-1, AX
        done:
                MOVQ AX, ret+8(FP)
                RET
        jnml@e5-1650:~/tmp/jumptable$ go run .
        0 100
        1 200
        2 300
        3 -1
        jnml@e5-1650:~/tmp/jumptable$