All Projects → Islam0mar → CL-CXX-JIT

Islam0mar / CL-CXX-JIT

Licence: other
Common Lisp and CXX interoperation with JIT

Programming Languages

C++
36643 projects - #6 most used programming language
common lisp
692 projects
emacs lisp
2029 projects

Projects that are alternatives of or similar to CL-CXX-JIT

Occa
JIT Compilation for Multiple Architectures: C++, OpenMP, CUDA, HIP, OpenCL, Metal
Stars: ✭ 230 (+475%)
Mutual labels:  jit
speckle-blender
speckle.systems/tag/blender/
Stars: ✭ 38 (-5%)
Mutual labels:  interoperability
crosscloudci
Integrating, testing, and deploying projects within the CNCF ecosystem. Results published daily to the CNCF CI Dashboard.
Stars: ✭ 83 (+107.5%)
Mutual labels:  interoperability
Asmjit
Machine code generation for C++
Stars: ✭ 2,874 (+7085%)
Mutual labels:  jit
PowerUp
⚡ Decompilation Tools and High Productivity Utilities ⚡
Stars: ✭ 1,526 (+3715%)
Mutual labels:  jit
lc
JIT compiler for Scheme targeting x86-64 platforms
Stars: ✭ 24 (-40%)
Mutual labels:  jit
Jfs
Constraint solver based on coverage-guided fuzzing
Stars: ✭ 215 (+437.5%)
Mutual labels:  jit
samp-plugin-jit
JIT plugin for SA-MP server (JIT compiler for Pawn 3.2)
Stars: ✭ 52 (+30%)
Mutual labels:  jit
FastLua
Lua trace JIT compiler using LLVM-C
Stars: ✭ 22 (-45%)
Mutual labels:  jit
CommonCoreOntologies
The Common Core Ontology Repository holds the current released version of the Common Core Ontology suite.
Stars: ✭ 109 (+172.5%)
Mutual labels:  interoperability
Lightbeam
Lightbeam has moved and now lives in the Wasmtime repository!
Stars: ✭ 253 (+532.5%)
Mutual labels:  jit
muparsersse
muparsersse a math parser for windows using just in time compilations of the expression
Stars: ✭ 14 (-65%)
Mutual labels:  jit
vox
Vox language compiler. AOT / JIT / Linker. Zero dependencies
Stars: ✭ 288 (+620%)
Mutual labels:  jit
Openj9
Eclipse OpenJ9: A Java Virtual Machine for OpenJDK that's optimized for small footprint, fast start-up, and high throughput. Builds on Eclipse OMR (https://github.com/eclipse/omr) and combines with the Extensions for OpenJDK for OpenJ9 repo.
Stars: ✭ 2,802 (+6905%)
Mutual labels:  jit
JUDI.jl
Julia Devito inversion.
Stars: ✭ 71 (+77.5%)
Mutual labels:  jit
Cranelift
Cranelift code generator
Stars: ✭ 2,485 (+6112.5%)
Mutual labels:  jit
Suravi
Suravi is a small distribution of Ravi/Lua 5.3 with batteries such as cjson, lpeglabel, luasocket, penlight, torch7, luv, luaossl
Stars: ✭ 56 (+40%)
Mutual labels:  jit
libjit
Unofficial libjit mirror.
Stars: ✭ 46 (+15%)
Mutual labels:  jit
Jitex
A library to modify MSIL and native code at runtime
Stars: ✭ 52 (+30%)
Mutual labels:  jit
cpptcl
C++ library for interoperability between C++ and TCL
Stars: ✭ 33 (-17.5%)
Mutual labels:  interoperability

CL-CXX-JIT - Common Lisp C++ JIT for exposing C++ functions

This library provides an interface to C++ from lisp. It compiles C++ code, then loads it into lisp. It was inspired by RCRL and the older project CL-CXX.

API

(from '("list of string headers") 'import "normal string is inserted as it is" '("function/function-pointer/mem-fun-pointer/lambda" . "name-used-in-lisp"))

Examples

SDL2 Example

sdl2.gif

(ql:quickload :cxx-jit)
(setf cxx-jit:*cxx-compiler-link-libs* "-lGL -lSDL2 -lSDL2main")

(cxx-jit:from '(
                "<SDL2/SDL.h>")
              'import
              '("[](){return SDL_Init(SDL_Init(SDL_INIT_VIDEO));}" . "init")
              '("SDL_CreateWindow" . "create-window")
              '("SDL_CreateRenderer" . "create-renderer")
              '("SDL_SetRenderDrawColor" . "set-color")
              '("SDL_DestroyWindow" . "destroy-window")
              '("SDL_RenderClear" . "clear-renderer")
              '("SDL_RenderPresent" . "renderer-render")
              '("SDL_Quit" . "sdl-quit"))

(init)
(setf wind (create-window "create-window" 0 0 600 700 0))
(setf rend (create-renderer wind -1 0))
(loop for x to (* 255 3)
      for r = (if (> x 255) 255 x)
      for g = (if (> x 255) (if (> x (* 2 255)) 255 (rem x 256)) 0)
      for b = (if (> x (* 2 255)) (rem x 256) 0)
      do
         (print x)
         (set-color rend r g b 255)
         (clear-renderer rend)
         (renderer-render rend)
         (sleep 0.01))

(destroy-window wind)
(sdl-quit)

Basic Example

Start with (ql:quickload :cxx-jit) (in-package cxx-jit)

(from '("<string>") 'import '("[](std::string x){return \"Hi, \"+x;}" . "hi"))
(hi "there!")

(from '("<cmath>") 'import '("static_cast<double(*)(double)>(std::sin)" . "cpp-sin"))
(cpp-sin 0d0)
(cpp-sin pi)
(cpp-sin (/ pi 2))

(from nil 'import "struct C{ auto hi(){return \"Hello, World\\n\";} auto bye(){return \"Bye\";} };" '("&C::bye" . "bye") '("&C::hi" . "hi") '("[](){static C x; return x;}" . "cc"))
(cc)
(hi *)
(bye **)
  ;;; structure  definition could be written in a header file then be used as the following:
(from '("c.hpp") 'import '("&C::bye" . "bye") '("&C::hi" . "hi") '("[](){static C x; return x;}" . "cc"))

Eigen Library Example

Start with (ql:quickload :cxx-jit) (in-package cxx-jit)

(from '("<Eigen/Core>" "<Eigen/Core>") 'import '("[](Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<double>,Eigen::Matrix<double, 3, 3>> x){
  std::stringstream s;
  s << x;
  return s.str();}" . "print-matrix"))

(from '("<Eigen/Core>" "<Eigen/Core>") 'import '("static_cast<const Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<double>,Eigen::Matrix<double, 3, 3>> (*)()> (&Eigen::Matrix3d::Identity)" . "identity-matrix"))

(print-matrix (identity-matrix))

Prerequisites

  • common lisp supporting CFFI
  • working C++17 compiler
  • the following variables should be set according to OS and compiler used
variabledefault value
*cxx-compiler-executable-path*/usr/bin/g++
*cxx-compiler-flags*-std=c++17 -Wall -Wextra -I/usr/include/eigen3
*cxx-compiler-working-directory*/tmp/” #\/ ‘/’ should be the last character
+cxx-compiler-lib-name+(intern ”plugin”)
+cxx-compiler-wrap-cxx-path+shouldn’t be changed ”path to wrap-cxx.cpp
*cxx-compiler-internal-flags*-shared -fPIC -Wl,--no-undefined -Wl,--no-allow-shlib-undefined
for g++ and for clang++ ”-shared -fPIC -Wl,-undefined,error -Wl,-flat_namespace
*cxx-compiler-link-libs*-lm” these flags are added after ”-o output” to link correctly
*cxx-type-name-to-cffi-type-symbol-alist*alist to map c++ type name to cffi type example:”(push (cons "long unsigned int" :ulong) *)

Installation

Clone into home/common-lisp directory. Then (ql:quickload :cxx-jit-test)

Supported Types

C++ typeLisp cffi type
fundamentalsame
string:string
class:pointer
std::is_function:pointer
othernot implemented!

Under The Hood

  • function/lambda/member_function/function_pointer is wrapped into a dummy lambda class to have a unique template specialization.
    Import([&]() { return __VA_ARGS__; });
        
  • Import function calls DecayThenResolve with function pointer as the template specialization so thunk pointer is omitted and we only return the direct function pointer which will be used from lisp side.
  • InvocableTypeName returns a vector contains: [return type, class type for class function member, args]. It resolves C++ types as follows:
    • Fundamental types and pointers are passed directly
    • String is converted to char* with new[] operator, should be cleared with ClCxxDeleteObject(ptr, true)
    • Class/std::is_function is converted to void* with new[] operator, should be cleared with ClCxxDeleteObject(ptr, false)
    • rest report an issue for other cases
  • Meta data for each function defined is passed through a lisp callback with this data:
    typedef struct {
      // could be void*
      void (*thunk_ptr)();
      bool method_p;
      const char **type;  // memory handled in C++
      std::uint8_t type_size;
    } MetaData;
        

NOTE

Tested on:

  • SBCL 2.0.1 on debian

Todo List

Add redirect stdout : freopen("/tmp/tmp.txt", "w", stdout); @apemangr

Use trivial-garbage with ClCxxDeleteObject

Add non-polling from

Test functions

Benchmark

Better class interface

Copyright

Copyright (c) 2021 Islam Omar ([email protected])

License

Licensed under the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].