1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use crate::bindings;
use std::ptr::null_mut;
use std::ffi::c_void;

pub enum Reg {
    R(bindings::jit_gpr_t),
    V(bindings::jit_gpr_t),
    F(bindings::jit_gpr_t),
}

pub struct JitNode<'a> {
    pub(crate) node:  *mut bindings::jit_node_t,
    pub(crate) phantom: std::marker::PhantomData<&'a ()>,
    //pub(crate) state: &'a JitState<'a>,
}

pub type JitWord = bindings::jit_word_t;
pub type JitUword = bindings::jit_uword_t;
pub type JitPointer = bindings::jit_pointer_t;

pub const NULL: JitPointer = null_mut::<c_void>();

pub(crate) trait ToFFI {
    type Type;
    fn to_ffi(&self) -> Self::Type;
}

impl ToFFI for Reg {
    type Type = bindings::jit_gpr_t;

    //TODO: safe conversion
    fn to_ffi(&self) -> Self::Type {
        match self {
            Reg::R(i) => if *i < unsafe { bindings::lgsys_JIT_R_NUM } {
                unsafe { bindings::lgsys_jit_r(*i) }
            } else {
                panic!("register 'R{}' is not supported", *i);
            },

            Reg::V(i) => if *i < unsafe { bindings::lgsys_JIT_V_NUM } {
                unsafe { bindings::lgsys_jit_v(*i) }
            } else {
                panic!("register 'V{}' is not supported", i);
            },

            Reg::F(i) => if *i < unsafe { bindings::lgsys_JIT_F_NUM } {
                unsafe { bindings::lgsys_jit_f(*i) }
            } else {
                panic!("register 'F{}' is not supported", i);
            },
        }
    }
}

impl<'a> ToFFI for JitNode<'a> {
    type Type = *mut bindings::jit_node_t;

    fn to_ffi(&self) -> Self::Type {
        self.node
    }
}

pub(crate) trait FFISafe: Copy{}

// this is dumb, blame rust
impl FFISafe for JitPointer{}
impl FFISafe for i32{}
impl FFISafe for u32{}
impl FFISafe for i64{}
impl FFISafe for u64{}
impl FFISafe for f32{}
impl FFISafe for f64{}


impl<T: FFISafe> ToFFI for T {
    type Type = T;

    fn to_ffi(&self) -> Self::Type {
        *self
    }
}