[ktap] [PATCHv2 0/8] FFI support for ktap

  • From: Qingping Hou <dave2008713@xxxxxxxxx>
  • To: ktap@xxxxxxxxxxxxx
  • Date: Fri, 29 Nov 2013 02:36:21 -0500

From: Qingping Hou <qingping.hou@xxxxxxxxx>

This patch set implements basic FFI support for ktap.

A brief overview of current design:
* user define needed C symbols in ktap script (functions, structs, etc)
* C symbols get compiled through cparser into chunk and passed into
ktap vm
* ktap vm generates ktap_cdata according to symbol information from
chunks
* when ktap script calls "C.foo()", ktap vm detects that it's a FFI
function, and does following:
  - sets up the hardware stack
  - calls into the C function
  - put back return value to ktap stack
* continue executing next instructions

Implementation of cparser is under userspace/ffi/, which currenly
supports parsing functions, structs and typedef.

Implementation of function call module is under interpreter/ffi/,
including argument check, type conversion, stack setup and return value
handling.

Support:
- x86_64 machine
- (u)char, (u)short, (u)int, (u)long, (u)longlong, void and pointer type
- 'cdef' keyword used to define external C functions
- implicit type conversion between ctypes and ktap types

Test:
New test script is under test/ffi/

Plan:
https://github.com/ktap/ktap/wiki/FFI-Support

Yicheng Qin (8):
  Introduce ffi_type, csymbol and ktap_cdata
  Cparser to parse C declareation
  Integrate cparser into ktap compiler
  Introduce ffi call module in vm
  Integrate ffi call module into vm
  update gitignore file for FFI related files
  Modify makefile to build ffi module
  Add ffi test cases and script

v2:
 * No csymbol allocation if there is no cdef in script
 * remove current test since it's useless without ffi.cast function
 * Userspace compiled without FFI support will emit chunk file without FFI
 csymbol (as a special case of FFI chunk)
 * VM compiled without FFI will exit on failure when seeing FFI enabled chunk
 file
 * Fix style in test/ffi/funct.c
 * Move global FFI state into ktap global_state

 .gitignore                    |    1 -
 Makefile                      |   26 +
 include/ktap_types.h          |   42 +-
 interpreter/ffi/call_x86_64.S |  139 +++
 interpreter/ffi/cdata.c       |   68 ++
 interpreter/ffi/ffi_call.c    |  361 ++++++++
 interpreter/ffi/ffi_symbol.c  |  172 ++++
 interpreter/ffi/ffi_type.c    |   50 ++
 interpreter/ffi/ffi_util.c    |   93 ++
 interpreter/kp_ffi.h          |  259 ++++++
 interpreter/kp_load.c         |   87 +-
 interpreter/kp_obj.c          |    6 +
 interpreter/kp_tab.c          |    2 +-
 interpreter/kp_vm.c           |   35 +-
 interpreter/ktap.h            |    9 +
 interpreter/lib_ffi.c         |   50 ++
 test/ffi/.gitignore           |    1 +
 test/ffi/Makefile             |   46 +
 test/ffi/cparser_test.c       |  304 +++++++
 test/ffi/funct.c              |   46 +
 test/ffi/halt.kp              |    3 +
 test/ffi/kfunct.kp            |    8 +
 test/ffi/ufunct.kp            |   24 +
 test/run_test.sh              |    2 +
 userspace/cparser.h           |  201 +++++
 userspace/dump.c              |   63 ++
 userspace/ffi/cparser.c       | 1894 +++++++++++++++++++++++++++++++++++++++++
 userspace/ffi/ctype.c         |  541 ++++++++++++
 userspace/ktapc.h             |   14 +
 userspace/lex.c               |    8 +-
 userspace/main.c              |    4 +
 userspace/parser.c            |   50 +-
 32 files changed, 4597 insertions(+), 12 deletions(-)
 create mode 100644 interpreter/ffi/call_x86_64.S
 create mode 100644 interpreter/ffi/cdata.c
 create mode 100644 interpreter/ffi/ffi_call.c
 create mode 100644 interpreter/ffi/ffi_symbol.c
 create mode 100644 interpreter/ffi/ffi_type.c
 create mode 100644 interpreter/ffi/ffi_util.c
 create mode 100644 interpreter/kp_ffi.h
 create mode 100644 interpreter/lib_ffi.c
 create mode 100644 test/ffi/.gitignore
 create mode 100644 test/ffi/Makefile
 create mode 100644 test/ffi/cparser_test.c
 create mode 100644 test/ffi/funct.c
 create mode 100644 test/ffi/halt.kp
 create mode 100644 test/ffi/kfunct.kp
 create mode 100644 test/ffi/ufunct.kp
 create mode 100644 userspace/cparser.h
 create mode 100644 userspace/ffi/cparser.c
 create mode 100644 userspace/ffi/ctype.c

-- 
1.8.1.2


Other related posts:

  • » [ktap] [PATCHv2 0/8] FFI support for ktap - Qingping Hou