FFI: FFI-Callbacks-mt.12.mcz

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

FFI: FFI-Callbacks-mt.12.mcz

commits-2
Marcel Taeumel uploaded a new version of FFI-Callbacks to project FFI:
http://source.squeak.org/FFI/FFI-Callbacks-mt.12.mcz

==================== Summary ====================

Name: FFI-Callbacks-mt.12
Author: mt
Time: 14 May 2021, 3:28:32.65676 pm
UUID: 51cbae8c-2817-eb44-b634-c1b7672455af
Ancestors: FFI-Callbacks-mt.11

Complements FFI-Kernel-mt.142

=============== Diff against FFI-Callbacks-mt.11 ===============

Item was removed:
- ----- Method: ExternalAddress class>>fromInteger: (in category '*FFI-Callbacks') -----
- fromInteger: anInteger
- "Read the given interger as an address pointing to an external memory area."
-
- | buffer type |
- self flag: #refactor. "Maybe: ExternalAddress new fromInteger: anInteger --- and maybe move to FFI-Kernel"
- type := ExternalType uintptr_t.
- buffer := ByteArray new: type byteSize.
- type handle: buffer at: 1 put: anInteger.
- ^ buffer asExternalPointer!

Item was changed:
  ----- Method: FFICallback class>>exampleCqsort04 (in category 'examples') -----
  exampleCqsort04
  "
  FFICallback exampleCqsort04
  "
 
  | type in out fn cb |
  type := ExternalType int32_t.
  in := type allocateExternal: 10.
  1 to: in size do: [:each |
  in at: each put: 100 atRandom].
 
  cb := FFICallback
  signature: '<callback: int (*)(int32_t* int32_t*)>'
  "signature: #(int 'double*' 'double*')"
  block: [ :arg1 :arg2 |
  | a  b |
  a := arg1 signedLongAt: 1.
  b := arg2 signedLongAt: 1.
  Transcript showln: ('Comparing {1} and {2}' format: {a. b}).
+
- "self halt."
  (a - b) sign].
 
  fn := ExternalLibraryFunction
  name: 'qsort' module: 'msvcrt.dll'
  callType: ExternalLibraryFunction callTypeCDecl
  returnType: ExternalType void
  argumentTypes: (ExternalType lookupTypes: #('void*' size_t size_t 'void*')).
 
  "Invoke!!"
+ [fn
- fn
  invokeWith: in "getHandle"
  with: in size
  with: in contentType byteSize
+ with: cb thunk "getHandle"]
+ ifCurtailed: [in free].
- with: cb thunk "getHandle".
 
  out := in collect: [:each | each].
  in free.
  ^ out!

Item was changed:
  ----- Method: FFICallback>>init__ccall_ARM32 (in category 'initialization - thunk prepare') -----
  init__ccall_ARM32
  <abi: #ARM32> <init>
  "Initialize the receiver with a __ccall thunk.  The thunk calls thunkEntry in the IA32ABI plugin,
  whose source is in platforms/Cross/plugins/IA32ABI/arm32abicc.c.  thunkEntry is the entry point
  for Callbacks. The objective of the thunk is to call thunkEntry with all arguments to the call of the
  thunk (registers and stack) as well as a pointer to the thunk itself.  thunkEntry is as follows:
 
  long long
  thunkEntry(long r0, long r1, long r2, long r3,
             double d0, double d1, double d2, double d3,
             double d4, double d5, double d6, double d7,
             void *thunkpPlus16, sqIntptr_t *stackp)
 
  thunkEntry then collects the various arguments (thunk, integer register arguments, floating-point
  register arguments, stack pointer) in a VMCallbackContext and invokes the callback via invokeCallbackContext:."
 
  "0x0 <thunk+0>: mov     r12, sp ; 0xe1a0c00d
    0x4 <thunk+4>: sub     sp, sp, #16 ; 0xe24dd010
    0x8 <thunk+8>: str     pc, [sp, #0] ; 0xe58df000 N.B. passes thunk+16; thunkEntry compensates
    0xc <thunk+12>: str     r12, [sp,#4] ; 0xe58dc004
    0x10 <thunk+16>: str     lr, [sp, #12] ; 0xe58de00c
    0x14 <thunk+20>: ldr     r12, [pc, #8] ; 0xe59fc008
    0x18 <thunk+24>: blx     r12 ; 0xe12fff3c
    0x1c <thunk+28>: add     sp, sp, #12 ; 0xe28dd00c
    0x20 <thunk+32>: ldr     pc, [sp], #4!! ; 0xe49df004 ; pop     {pc}
    0x24 <thunk+36>:     .word thunkEntry"
  self flag: #hidden. "mt: How is the thunk's handle stored to lookup this instance upon callback later?"
  thunk getHandle
  unsignedLongAt:   1 put: 16re1a0c00d;
  unsignedLongAt:   5 put: 16re24dd010;
  unsignedLongAt:   9 put: 16re58df000;
  unsignedLongAt: 13 put: 16re58dc004;
  unsignedLongAt: 17 put: 16re58de00c;
  unsignedLongAt: 21 put: 16re59fc008;
  unsignedLongAt: 25 put: 16re12fff3c;
  unsignedLongAt: 29 put: 16re28dd00c;
  unsignedLongAt: 33 put: 16re49df004;
+ pointerAt: 37 put: self thunkEntryAddress length: 4.!
- shortPointerAt: 37 put: self thunkEntryAddress.!

Item was changed:
  ----- Method: FFICallback>>init__ccall_IA32 (in category 'initialization - thunk prepare') -----
  init__ccall_IA32
  <abi: #IA32> <init>
  "Initialize the receiver with a __ccall thunk.  The thunk calls thunkEntry in the IA32ABI plugin,
  whose source is in platforms/Cross/plugins/IA32ABI/x64win64abicc.c.  thunkEntry is the entry point
  for Callbacks. The objective of the thunk is to call thunkEntry with all arguments to the call of the
  thunk (registers and stack) as well as a pointer to the thunk itself.  thunkEntry is as follows:
 
  long
  thunkEntry(void *thunkp, sqIntptr_t *stackp)
 
  thunkEntry then collects the various arguments (thunk, stack pointer) in a VMCallbackContext and
  invokes the callback via invokeCallbackContext:."
 
  "thunk: push   %esp 0x54 0xa1905454
  thunk+01: push   %esp 0x54
  thunk+02: nop   0x90
  thunk+03: mov    $thunkEntry,%eax 0xb8 0x00 0x00 0x00 0x00 0x00000000 - entry
  thunk+08: nop   0x90 0x68909090
  thunk+09: nop   0x90
  thunk+10: nop   0x90
  thunk+11: push   $thunk 0x68 0x00 0x00 0x00 0x00 0x00000000 - thunk
  thunk+16: call   *%eax 0xff 0xd0 0xc483d0ff
  thunk+18: add    $0xC,%esp 0x83 0xc4 0x0C 0x9090c30C
  thunk+21: ret 0xc3
  thunk+22: nop 0x90
  thunk+23: nop 0x90"
  thunk getHandle
  unsignedLongAt:  1 put: 16rB8905454;
+ pointerAt: 5 put: self thunkEntryAddress length: 4;
- shortPointerAt: 5 put: self thunkEntryAddress;
  unsignedLongAt:  9 put: 16r68909090;
+ pointerAt: 13 put: thunk getHandle length: 4;
- shortPointerAt: 13 put: thunk getHandle;
  unsignedLongAt: 17 put: 16rC483D0FF;
  unsignedLongAt: 21 put: 16r9090C30C!

Item was changed:
  ----- Method: FFICallback>>init__ccall_X64 (in category 'initialization - thunk prepare') -----
  init__ccall_X64
  <abi: #X64> <init>
  "Initialize the receiver with a __ccall thunk.  The thunk calls thunkEntry in the IA32ABI plugin,
  whose source is in platforms/Cross/plugins/IA32ABI/x64sysvabicc.c.  thunkEntry is the entry point
  for Callbacks. The objective of the thunk is to call thunkEntry with all arguments to the call of the
  thunk (registers and stack) as well as a pointer to the thunk itself.  thunkEntry is as follows:
 
  long
  thunkEntry(long a0, long a1, long a2, long a3, long a4, long a5,
             double d0, double d1, double d2, double d3,
             double d4, double d5, double d6, double d7,
             void *thunkp, sqIntptr_t *stackp)
 
  thunkEntry then collects the various arguments (thunk, integer register arguments, floating-point
  register arguments, stack pointer) in a VMCallbackContext and invokes the callback via invokeCallbackContext:."
 
  "thunk+0x0: pushq  %rsp 54
  thunk+0x1: pushq  %rsp 54
  thunk+0x4: movabsq $thunk, %rax 48 b8 b0..b7 eight bytes of thunk address a.k.a. handle
  thunk+0xc: pushq  %rax 50
  thunk+0xd: movabsq $thunkEntry, %rax 48 b8 b0..b7 eight bytes of the thunkEntry address
  thunk+0x17: callq  *%rax ff d0
  thunk+0x19: addq   $0x18, %rsp 48 83 c4 18
  thunk+0x1d: retq c3
  thunk+0x1e: nop 90
  thunk+0x1f: nop 90"
  thunk getHandle
  unsignedLongAt:  1 put: 16rb8485454;
+ pointerAt:  5 put: thunk getHandle length: 8;
- longPointerAt:  5 put: thunk getHandle;
  unsignedLongAt: 13 put: 16r00b84850; "00 is the first byte of the 64-bit constant the movabsq/0x48 opcode moves"
+ pointerAt: 16 put: self thunkEntryAddress length: 8;
- longPointerAt: 16 put: self thunkEntryAddress;
  unsignedByteAt: 24 put: 16rff;
  unsignedLongAt: 25 put: 16rc48348d0;
  unsignedLongAt: 29 put: 16r9090c318.!

Item was changed:
  ----- Method: FFICallback>>init__ccall_X64Win64 (in category 'initialization - thunk prepare') -----
  init__ccall_X64Win64
  <abi: #X64Win64> <init>
  "Initialize the receiver with a __ccall thunk.  The thunk calls thunkEntry in the IA32ABI plugin,
  whose source is in platforms/Cross/plugins/IA32ABI/x64win64abicc.c.  thunkEntry is the entry point
  for Callbacks. The objective of the thunk is to call thunkEntry with all arguments to the call of the
  thunk (registers and stack) as well as a pointer to the thunk itself.  thunkEntry is as follows:
 
  long long
  thunkEntry(long long rcx, long long rdx,
        long long r8, long long r9,
  void *thunkp, sqIntptr_t *stackp)
 
  thunkEntry then collects the various arguments (thunk, integer register arguments, stack pointer)
  in a VMCallbackContext and invokes the callback via invokeCallbackContext:."
 
  "thunk+0x0: pushq  %rsp 54
  thunk+0x1: pushq  %rsp 54
  thunk+0x4: movabsq $thunk, %rax 48 b8 b0..b7 eight bytes of thunk address a.k.a. addressField
  thunk+0xc: pushq  %rax 50
  thunk+0xd: subq   $0x20, %rsp 48 83 c4 e0 (this is addq -20 since the immediate is signed extended)
  thunk+0x11: movabsq $thunkEntry, %rax 48 b8 b0..b7 eight bytes of the thunkEntry address
  thunk+0x1b: callq  *%rax ff d0
  thunk+0x1d: addq   $0x38, %rsp 48 83 c4 38
  thunk+0x21: retq c3
  thunk+0x22: nop 90
  thunk+0x23: nop 90"
  thunk getHandle
  unsignedLongAt:  1 put: 16rb8485454;
+ pointerAt:  5 put: thunk getHandle length: 8;
- longPointerAt:  5 put: thunk getHandle;
  unsignedLongAt: 13 put: 16rc4834850;
  unsignedLongAt: 17 put: 16r00b848e0; "00 is the first byte of the 64-bit constant the movabsq/0x48 opcode moves"
+ pointerAt: 20 put: self thunkEntryAddress length: 8;
- longPointerAt: 20 put: self thunkEntryAddress;
  unsignedByteAt: 28 put: 16rff;
  unsignedLongAt: 29 put: 16rc48348d0;
  unsignedLongAt: 33 put: 16r9090c338.!

Item was changed:
  ----- Method: FFICallback>>init__stdcall_IA32: (in category 'initialization - thunk prepare') -----
  init__stdcall_IA32: numBytes
  <abi: #IA32> <init>
  "Initialize the receiver with a __stdcall thunk with numBytes argument bytes. (See #init__ccall_IA32 for more info)"
  "thunk: push   %esp 0x54 0xa1905454
  thunk+01: push   %esp 0x54
  thunk+02: nop   0x90
  thunk+03: mov    $thunkEntry,%eax 0xb8 0x00 0x00 0x00 0x00 0x00000000 - entry
  thunk+08: nop   0x90 0x68909090
  thunk+09: nop   0x90
  thunk+10: nop   0x90
  thunk+11: push   $thunk 0x68 0x00 0x00 0x00 0x00 0x00000000 - thunk
  thunk+16: call   *%eax 0xff 0xd0 0xc483d0ff
  thunk+18: add    $0xC,%esp 0x83 0xc4 0x0C 0xBYTSc20C
  thunk+21: ret    $bytes 0xc2 0xBY 0xTS"
 
  thunk getHandle
  unsignedLongAt:  1 put: 16rB8905454;
+ pointerAt: 5 put: self thunkEntryAddress length: 4;
- shortPointerAt: 5 put: self thunkEntryAddress;
  unsignedLongAt:  9 put: 16r68909090;
+ pointerAt: 13 put: thunk getHandle length: 4;
- shortPointerAt: 13 put: thunk getHandle;
  unsignedLongAt:  17 put: 16rC483D0FF;
  unsignedShortAt: 21 put: 16rC20C;
  unsignedShortAt: 23 put: numBytes.!