use std::{borrow::Cow, collections::HashMap, fmt::Debug};
use crate::rt::Value;
use super::{code_space::CodeBuffer, graph::Signature};
pub trait CompiledCode: Debug + Send + Sync + 'static {
fn symbol(&self) -> &Symbol;
fn signature(&self) -> &Signature;
fn code_buffer(&self) -> &CodeBuffer;
fn entry_offset(&self) -> usize;
fn entry(&self) -> *const u8 {
&self.code_buffer()[self.entry_offset()] as *const u8
}
fn get_call_site(&self, return_address: *const u8) -> Option<&dyn CallSite>;
fn link(&self, symbols: &HashMap<Symbol, *const u8>);
fn invoke(&self, args: &[Value]) -> Option<Value>;
fn dump(&self);
}
pub trait CallSite: Send + Sync {
fn symbol(&self) -> &Symbol;
fn patch(&self, addr: *const u8);
}
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub enum Symbol {
NamedFn(Cow<'static, str>),
NumberedFn(usize),
ExternFn(*const u8),
}
impl Symbol {
pub fn named(name: &'static str) -> Self {
Self::NamedFn(Cow::Borrowed(name))
}
}
unsafe impl Send for Symbol {}
unsafe impl Sync for Symbol {}