feat: JIT (WIP)

This commit is contained in:
2025-05-17 20:54:36 +08:00
parent c3d365d486
commit 95ebddf272
15 changed files with 175 additions and 274 deletions

View File

@@ -1,79 +0,0 @@
use std::hash::Hash;
use derive_more::{IsVariant, Unwrap};
use ecow::EcoString;
#[derive(Debug, Clone, IsVariant, Unwrap)]
pub enum Const {
Bool(bool),
Int(i64),
Float(f64),
String(EcoString),
Null,
}
impl Hash for Const {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
use Const::*;
match self {
Int(x) => x.hash(state),
Float(x) => x.to_bits().hash(state),
Bool(x) => x.hash(state),
String(x) => x.hash(state),
x @ Null => x.hash(state),
}
}
}
impl From<bool> for Const {
fn from(value: bool) -> Self {
Const::Bool(value)
}
}
impl From<i64> for Const {
fn from(value: i64) -> Self {
Const::Int(value)
}
}
impl From<f64> for Const {
fn from(value: f64) -> Self {
Const::Float(value)
}
}
impl From<EcoString> for Const {
fn from(value: EcoString) -> Self {
Const::String(value)
}
}
impl From<String> for Const {
fn from(value: String) -> Self {
Const::String(value.into())
}
}
impl From<&str> for Const {
fn from(value: &str) -> Self {
Const::String(value.into())
}
}
impl PartialEq for Const {
fn eq(&self, other: &Self) -> bool {
use Const::*;
match (self, other) {
(Bool(a), Bool(b)) => a == b,
(Int(a), Int(b)) => a == b,
(Float(a), Float(b)) => a == b,
(Int(a), Float(b)) => *a as f64 == *b,
(Float(a), Int(b)) => *b as f64 == *a,
(String(a), String(b)) => a == b,
_ => false,
}
}
}
impl Eq for Const {}

View File

@@ -1,9 +1,12 @@
use derive_more::Constructor;
use std::cell::{Cell, OnceCell};
use itertools::Itertools;
use derive_more::Constructor;
use crate::bytecode::Func as BFunc;
use crate::error::Result;
use crate::ir;
use crate::jit::JITFunc;
use crate::ty::internal::{Thunk, Value};
use crate::vm::{Env, VM};
@@ -37,14 +40,12 @@ impl From<ir::Param> for Param {
}
}
pub type JITFunc<'vm> =
unsafe extern "C" fn(vm: *mut VM<'_>, *mut Env<'vm>, *mut Value<'vm>) -> Value<'vm>;
#[derive(Debug, Clone, Constructor)]
pub struct Func<'vm> {
pub func: &'vm BFunc,
pub env: Env<'vm>,
pub compiled: Option<JITFunc<'vm>>,
pub compiled: OnceCell<JITFunc>,
pub count: Cell<usize>
}
impl<'vm> Func<'vm> {

View File

@@ -6,7 +6,7 @@ use std::rc::Rc;
use derive_more::{IsVariant, Unwrap};
use super::common as c;
use super::common::*;
use super::public as p;
use crate::bytecode::OpCodes;
@@ -14,14 +14,12 @@ use crate::error::*;
use crate::vm::{Env, VM};
mod attrset;
mod cnst;
mod func;
mod list;
mod primop;
mod string;
pub use attrset::*;
pub use cnst::Const;
pub use func::*;
pub use list::List;
pub use primop::*;
@@ -33,7 +31,7 @@ pub enum Value<'vm> {
ThunkRef(&'vm Thunk<'vm>),
AttrSet(Rc<AttrSet<'vm>>),
List(Rc<List<'vm>>),
Catchable(c::Catchable),
Catchable(Catchable),
PrimOp(Rc<PrimOp<'vm>>),
PartialPrimOp(Rc<PartialPrimOp<'vm>>),
Func(Rc<Func<'vm>>),
@@ -81,7 +79,7 @@ pub enum ValueAsRef<'v, 'vm: 'v> {
Thunk(&'v Thunk<'vm>),
AttrSet(&'v AttrSet<'vm>),
List(&'v List<'vm>),
Catchable(&'v c::Catchable),
Catchable(&'v Catchable),
PrimOp(&'v PrimOp<'vm>),
PartialPrimOp(&'v PartialPrimOp<'vm>),
Func(&'v Func<'vm>),
@@ -93,7 +91,7 @@ pub enum ValueAsMut<'v, 'vm: 'v> {
Thunk(&'v Thunk<'vm>),
AttrSet(&'v mut AttrSet<'vm>),
List(&'v mut List<'vm>),
Catchable(&'v mut c::Catchable),
Catchable(&'v mut Catchable),
PrimOp(&'v mut PrimOp<'vm>),
PartialPrimOp(&'v mut PartialPrimOp<'vm>),
Func(&'v Func<'vm>),