Skip to content

Instantly share code, notes, and snippets.

@fowlmouth
Created December 7, 2013 13:23
Show Gist options
  • Save fowlmouth/7841229 to your computer and use it in GitHub Desktop.
Save fowlmouth/7841229 to your computer and use it in GitHub Desktop.
when defined(LINUX):
const libName* = "libjit.so"
type
PContext* = ptr object
PFunction* = ptr object
PBlock* = ptr object
PInstruction* = ptr object
PValue* = ptr object
PType* = ptr object
PStackTrace* = ptr object
PLabel* = culong
TABI* {.size: sizeof(cint).} = enum
ABI_Cdecl, ABI_Vararg, ABI_StdCall, ABI_FastCall
JIT_int* = cint
{.push callconv: cdecl, importc: "jit_$1".}
when defined(LinkStatically):
{.passl: "-ljit".}
{.push header: "<jit/jit.h>".}
else:
{.push dynlib: libname.}
# jit-context.h
proc context_create * : PContext
proc context_destroy* (context: PContext)
proc context_build_start* (context: PContext)
proc context_build_end* (context: PContext)
# jit-init.h
proc init*
proc uses_interpreter*: cint
proc supports_threads*: cint
proc supports_virtual_memory*: cint
proc supports_closures*: cint
proc get_closure_size*: cuint
proc get_closure_alignment*: cuint
proc get_trampoline_size*: cuint
proc get_trampoline_alignment*: cuint
# jit-function.h
proc function_create* (context: PContext; signature: PType): PFunction
proc function_compile* (func: PFunction): cint
proc function_apply* (func: PFunction; args: ptr pointer; returnvalue: pointer): cint
# jit-insn.h
proc insn_add* (func: PFunction; value1, value2: PValue): PValue
proc insn_mul* (func: PFunction; value1, value2: PValue): PValue
proc insn_return* (func: PFunction; value: PValue): cint
# jit-type.h
var
type_int: PType
proc type_create_signature* (
abi: TABI; returnType: PType;
params: ptr PType; num_params: cuint, incref: cint): PType
proc type_free* (ty: PType)
# jit-value.h
proc value_get_param* (
func: PFunction; param: cuint): PValue
{.pop.}
{.pop.}
when isMainModule:
## example:
discard """
int mul_add (int x, int y, int z) {
return x * y + z;
} """
var
context: PContext
params: array[3, PType]
signature: PType
function: PFunction
x, y, z: PValue
temp1, temp2: PValue
#/* Create a context to hold the JIT's primary state */
context = context_create();
#/* Lock the context while we build and compile the function */
context_build_start(context);
#/* Build the function signature */
params[0] = type_int;
params[1] = type_int;
params[2] = type_int;
signature = type_create_signature(
abi_cdecl, type_int, params[0].addr, 3, 1);
#/* Create the function object */
function = function_create(context, signature);
type_free(signature);
#/* Construct the function body */
x = value_get_param(function, 0);
y = value_get_param(function, 1);
z = value_get_param(function, 2);
temp1 = insn_mul(function, x, y);
temp2 = insn_add(function, temp1, z);
echo insn_return(function, temp2);
#/* Compile the function */
echo function_compile(function);
#/* Unlock the context */
context_build_end(context);
var
arg1, arg2, arg3, result: jitInt
args: array[3, pointer]
#/* Execute the function and print the result */
arg1 = 3;
arg2 = 5;
arg3 = 2;
args[0] = addr arg1;
args[1] = addr arg2;
args[2] = addr arg3;
echo function_apply(function, args[0].addr, addr result);
echo "mul_add(3, 5, 2) = ", result
#/* Clean up */
context_destroy(context);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment