github.com/notti/nocgo@v0.0.0-20190619201224-fc443047424c/internal/ffi/call_386.s (about)

     1  #include "textflag.h"
     2  #include "go_asm.h"
     3  
     4  // runtime has #include "go_asm.h"
     5  // we need to fake the defines here:
     6  #define slice_array 0
     7  #define slice_len 4
     8  #define slice_cap 8
     9  
    10  TEXT ·cgocall(SB),NOSPLIT,$0
    11      JMP runtime·cgocall(SB)
    12  
    13  // pass struct { &args, &spec } to cgocall
    14  TEXT ·callWrapper(SB),NOSPLIT|WRAPPER,$16
    15      MOVL DX, 12(SP)
    16      LEAL argframe+0(FP), AX
    17      MOVL AX, 8(SP)
    18      LEAL 8(SP), AX
    19      MOVL AX, 4(SP)
    20      LEAL asmcall(SB), AX
    21      MOVL AX, 0(SP)
    22      CALL ·cgocall(SB)
    23      RET
    24  
    25  TEXT asmcall(SB),NOSPLIT,$0-4
    26      MOVL frame+0(FP), SI // &args, &spec (preserved)
    27      MOVL SP, DI          // STACK (preserved)
    28  
    29      MOVL 4(SI), AX       // spec
    30      MOVL spec_stack+slice_len(AX), BX
    31      TESTL BX, BX
    32      JZ prepared
    33  
    34      MOVL spec_stack+slice_array(AX), DX
    35  
    36  next:
    37      DECL BX
    38      MOVL (DX)(BX*argument__size), AX
    39      MOVWLZX AX, CX
    40      SHRL $16, AX
    41  
    42  #define TYPE(which, instr) \
    43      CMPB AX, which \
    44      JNE 9(PC) \
    45      SUBL $4, SP \
    46      MOVL 0(SI), AX \
    47      ADDL CX, AX \
    48      instr 0(AX), AX \
    49      MOVL AX, 0(SP) \
    50      TESTL BX, BX \
    51      JZ prepared \
    52      JMP next
    53  
    54      TYPE($const_type32, MOVL)
    55      TYPE($const_type16, MOVW)
    56      TYPE($const_type8, MOVB)
    57      INT $3
    58  
    59  prepared:
    60  
    61      MOVL 4(SI), AX
    62      CALL spec_fn(AX)
    63  
    64      // return value in AX, DX, F0 <- DON'T USE THESE
    65  
    66      MOVL DI, SP // restore stack
    67  
    68      MOVL 4(SI), DI // DI: spec
    69  
    70      MOVL spec_ret(DI), BX
    71      TESTL BX, BX
    72      JS done
    73  
    74      MOVL 0(SI), SI // SI: args
    75  
    76      // TODO: check SI, if it still points to the correct stack! (could happen if we have a callback into go that splits the stack)
    77  
    78      MOVWLZX BX, CX
    79      SHRL $16, BX
    80  
    81      CMPB BX, $const_type32
    82      JNE 4(PC)
    83      ADDL SI, CX
    84      MOVL AX, (CX)
    85      JMP done
    86  
    87      CMPB BX, $const_type16
    88      JNE 4(PC)
    89      ADDL SI, CX
    90      MOVW AX, (CX)
    91      JMP done
    92  
    93      CMPB BX, $const_type8
    94      JNE 4(PC)
    95      ADDL SI, CX
    96      MOVB AX, (CX)
    97      JMP done
    98  
    99      CMPB BX, $const_typeFloat
   100      JNE 4(PC)
   101      ADDL SI, CX
   102      FMOVF F0, (CX)
   103      JMP done
   104  
   105      CMPB BX, $const_typeDouble
   106      JNE 4(PC)
   107      ADDL SI, CX
   108      FMOVD F0, (CX)
   109      JMP done
   110  
   111      CMPB BX, $const_type64
   112      JNE 6(PC)
   113      ADDL SI, CX
   114      MOVL AX, (CX)
   115      ADDL $4, CX
   116      MOVL DX, (CX)
   117      JMP done
   118  
   119      INT $3
   120  
   121  done:
   122  
   123      RET