minio / c2goasm

C to Go Assembly
Apache License 2.0
1.31k stars 110 forks source link

Can companion declarations be avoided for private functions? #13

Open starius opened 6 years ago

starius commented 6 years ago

I'm trying to convert a big library which has many functions calling each other and just few of them are public. Nevertheless I have to write a lot of declarations in the companion file for all those private assembly functions, which is boring. Are they really needed for private functions?

fwessels commented 6 years ago

If you don't need them from Golang you should be able to leave them out. And maybe really make sure that they are private, or even inlined by the compiler.

starius commented 6 years ago

I marked functions static and c2goasm just skipped them.

Note that I applied my fix from https://github.com/minio/c2goasm/issues/15 to fix the crash.

$ cat f.c
static int foo(int (*f)(int), int arg) {
    return f(arg) + 1;
}

static int bar(int x) {
    return x * 2;
}

int buz() {
    return foo(bar, 5);
}

$ clang-6.0 -O0 -mavx -mfma -masm=intel -fno-asynchronous-unwind-tables -fno-exceptions -fno-rtti -S f.c -o f.s                                                                           

$ cat f.s
        .text
        .intel_syntax noprefix
        .file   "f.c"
        .globl  buz                     # -- Begin function buz
        .p2align        4, 0x90
        .type   buz,@function
buz:                                    # @buz
# %bb.0:
        push    rbp
        mov     rbp, rsp
        movabs  rdi, offset bar
        mov     esi, 5
        call    foo
        pop     rbp
        ret
.Lfunc_end0:
        .size   buz, .Lfunc_end0-buz
                                        # -- End function
        .p2align        4, 0x90         # -- Begin function foo
        .type   foo,@function
foo:                                    # @foo
# %bb.0:
        push    rbp
        mov     rbp, rsp
        sub     rsp, 32
        mov     qword ptr [rbp - 8], rdi
        mov     dword ptr [rbp - 12], esi
        mov     rdi, qword ptr [rbp - 8]
        mov     esi, dword ptr [rbp - 12]
        mov     qword ptr [rbp - 24], rdi # 8-byte Spill
        mov     edi, esi
        mov     rax, qword ptr [rbp - 24] # 8-byte Reload
        call    rax
        add     eax, 1
        add     rsp, 32
        pop     rbp
        ret
.Lfunc_end1:
        .size   foo, .Lfunc_end1-foo
                                        # -- End function
        .p2align        4, 0x90         # -- Begin function bar
        .type   bar,@function
bar:                                    # @bar
# %bb.0:
        push    rbp
        mov     rbp, rsp
        mov     dword ptr [rbp - 4], edi
        mov     edi, dword ptr [rbp - 4]
        shl     edi, 1
        mov     eax, edi
        pop     rbp
        ret
.Lfunc_end2:
        .size   bar, .Lfunc_end2-bar
                                        # -- End function

        .ident  "clang version 6.0.0-1~bpo9+1 (tags/RELEASE_600/final)"
        .section        ".note.GNU-stack","",@progbits

$ c2goasm -a f.s gofunc/f_amd64.s
Processing f.s
Invoking asm2plan9s on gofunc/f_amd64.s

$ cat ./gofunc/f_amd64.s
//+build !noasm !appengine
// AUTO-GENERATED BY C2GOASM -- DO NOT EDIT

TEXT ·_buz(SB), $0-8

    LONG $0x00c7c748; WORD $0x0000; BYTE $0x00 // mov    rdi, offset bar
    LONG $0x000005be; BYTE $0x00 // mov    esi, 5
        CALL foo
    MOVQ AX, result+0(FP)
    RET

$ go get ./gofunc/
# func-call/gofunc
gofunc/f_amd64.s:12: undefined label foo
asm: invalid instruction: 00039 (gofunc/f_amd64.s:9)   CALL
asm: assembly failed
lrita commented 6 years ago

The ABI of go asm is different with C. I think the tool is missing concern this.