package gccjit

  1. Overview
  2. Docs
Bindings to libgccjit, the GCC JIT compiler

Install

Dune Dependency

Authors

Maintainers

Sources

0.3.2.1.tar.gz
md5=02d93b7c984e70faf41a3868db4f60b3
sha512=32e95f713b9f8601c373abb43974ebfe67dd4ce4045a8fcc7100b3a7818743725996ddb2b27ba5a267ef0c23f75fde3269ef81799d652dd61c0302b497f9468c

Description

libgccjit is an embeddable shared library being included in GCC for adding compilation to existing programs using GCC as the backend. This package organizes the API of gccjit into a few types and their corresponding modules.

Tags

gcc jit gccjit

Published: 26 Jan 2024

README

ocaml-gccjit

ocaml-gccjit is a OCaml library that provides bidings for libgccjit. libgccjit is an embeddable shared library available since GCC 5 for adding compilation to existing programs using GCC as the backend.

For example, consider this C function:

int square (int i)
{
  return i * i;
}

We can construct this function at runtime using libgccjit, as follows:

open Gccjit

let square =
  let ctx = Context.create () in

  (* Create parameter "i" *)
  let param_i = Param.create ctx Type.(get ctx Int) "i" in

  (* Create the function *)
  let fn = Function.create ctx Function.Exported Type.(get ctx Int) "square" [ param_i ] in

  (* Create a basic block within the function *)
  let block = Block.create ~name:"entry" fn in

  (* This basic block is relatively simple *)
  let expr = RValue.binary_op ctx Mult Type.(get ctx Int) (RValue.param param_i) (RValue.param param_i) in
  Block.return block expr;

  (* Having populated the context, compile it *)
  let jit_result = Context.compile ctx in

  (* Look up a specific machine code routine within the gccjit.Result, in this
     case, the function we created above: *)
  Result.code jit_result "square" Ctypes.(int @-> returning int)

We can now call the function by doing simply

(* Now try running the code *)
Printf.printf "square(5) = %d\n%!" (square 5)

Installation

Either opam install gccjit or:


opam pin add gccjit git://github.com/lukstafi/ocaml-gccjit

Installing the package should also install the libgccjit library. If that is unsuccessful, install libgccjit manually so that it is found by the C compiler using the -lgccjit flag.

Links

Contact

Nicolas Ojeda Bar: n.oje.bar@gmail.com Lukasz Stafiniak: lukstafi@gmail.com

Dependencies (6)

  1. conf-libgccjit
  2. ctypes-foreign
  3. ctypes >= "0.14.0"
  4. base-unix
  5. dune >= "3.11"
  6. ocaml >= "4.08.0"

Dev Dependencies (2)

  1. odoc with-doc
  2. ppx_expect with-test & >= "v0.9.0"

Used by

None