Skip to content

Commit

Permalink
Apply suggested changes. Fix cspell.
Browse files Browse the repository at this point in the history
  • Loading branch information
FranchuFranchu committed Feb 15, 2025
1 parent 7f34afd commit 20a7ab9
Show file tree
Hide file tree
Showing 13 changed files with 224 additions and 228 deletions.
4 changes: 4 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ workspace = true
[features]
vine = ["dep:vine", "dep:vine-lsp"]
default = ["vine"]

[[bin]]
name = "vine"
required-features = ["vine"]
32 changes: 15 additions & 17 deletions cli/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,22 @@ pub struct RunArgs {

impl RunArgs {
pub fn run(self, nets: Nets) {
{
let mut host = &mut Host::default();
let heap = Heap::new();
let mut extrinsics = Extrinsics::default();
let mut host = &mut Host::default();
let heap = Heap::new();
let mut extrinsics = Extrinsics::default();

host.register_default_extrinsics(&mut extrinsics);
host.insert_nets(&nets);
let main = host.get("::main").expect("missing main");
let mut ivm = IVM::new(&heap, &extrinsics);
ivm.boot(main, host.new_io());
if self.workers > 0 {
ivm.normalize_parallel(self.workers)
} else {
ivm.normalize();
}
if !self.no_stats {
eprintln!("{}", ivm.stats);
}
host.register_default_extrinsics(&mut extrinsics);
host.insert_nets(&nets);
let main = host.get("::main").expect("missing main");
let mut ivm = IVM::new(&heap, &extrinsics);
ivm.boot(main, host.new_io());
if self.workers > 0 {
ivm.normalize_parallel(self.workers)
} else {
ivm.normalize();
}
if !self.no_stats {
eprintln!("{}", ivm.stats);
}
}
}
2 changes: 1 addition & 1 deletion ivm/src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<'ivm> Allocator<'ivm> {
}
}

impl<'ext, 'ivm> IVM<'ext, 'ivm> {
impl<'ivm, 'ext> IVM<'ivm, 'ext> {
/// Allocates a new binary node with a given `tag` and `label`.
///
/// ## Safety
Expand Down
179 changes: 13 additions & 166 deletions ivm/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,203 +6,50 @@
use core::{
fmt::{self, Debug},
marker::PhantomData,
ops::{Add, Div, Mul, Sub},
};
use std::{
collections::HashMap,
io::{self, Read, Write},
};

use crate::port::Tag;

macro_rules! define_ext_fns {
($map:ident,$self:ident, $($name:expr => |$a:ident, $b: ident| $body:expr ),*) => {
$($map.insert($name.to_string(), $self.register_ext_fn(move |[$a, $b]| [$body]).unwrap());)*
};
}
#[derive(Default)]
pub struct Extrinsics<'ivm> {
ext_fns: Vec<Box<dyn Fn(ExtVal<'ivm>, ExtVal<'ivm>) -> ExtVal<'ivm> + Sync + 'ivm>>,
// amount of registered light exttys
// amount of registered light (unboxed) ext types
light_ext_ty: u16,

n32_ext_ty: Option<ExtTy<'ivm>>,
io_ext_ty: Option<ExtTy<'ivm>>,

phantom: PhantomData<fn(&'ivm ()) -> &'ivm ()>,
}

impl<'ivm> Extrinsics<'ivm> {
pub const MAX_EXT_FN_KIND_COUNT: usize = 0x8FFF;
pub const MAX_LIGHT_EXT_TY_COUNT: usize = 0x8FFF;
pub const MAX_RC_EX_TTY_COUNT: usize = 0x8FFF;
pub const MAX_EXT_FN_KIND_COUNT: usize = 0x7FFF;
pub const MAX_LIGHT_EXT_TY_COUNT: usize = 0x7FFF;

pub fn register_ext_fn(
&mut self,
f: impl Fn([ExtVal<'ivm>; 2]) -> [ExtVal<'ivm>; 1] + Sync + 'ivm,
) -> Option<ExtFn<'ivm>> {
) -> ExtFn<'ivm> {
if self.ext_fns.len() >= Self::MAX_EXT_FN_KIND_COUNT {
None
panic!("IVM reached maximum amount of registered extrinsic functions.");
} else {
let ext_fn = ExtFn(self.ext_fns.len() as u16, PhantomData);
self.ext_fns.push(Box::new(move |a, b| f([a, b])[0]));
Some(ext_fn)
ext_fn
}
}
pub fn register_light_ext_ty(&mut self) -> Option<ExtTy<'ivm>> {
pub fn register_light_ext_ty(&mut self) -> ExtTy<'ivm> {
if self.light_ext_ty as usize >= Self::MAX_LIGHT_EXT_TY_COUNT {
None
panic!("IVM reached maximum amount of registered extrinsic unboxed types.");
} else {
let ext_ty = ExtTy::from_id_and_rc(self.light_ext_ty, false);
self.light_ext_ty += 1;
Some(ext_ty)
ext_ty
}
}
/// Some extrinsics are built-in with IVM
pub fn register_builtin_extrinsics(
&mut self,
) -> (HashMap<String, ExtFn<'ivm>>, HashMap<String, ExtTy<'ivm>>) {
let mut ext_fn_map = HashMap::new();
let mut ext_ty_map = HashMap::new();
let n32_ext_ty = self.register_light_ext_ty().expect("reached maximum amount of extrinsics!");
let f32_ext_ty = self.register_light_ext_ty().expect("reached maximum amount of extrinsics!");
let io_ext_ty = self.register_light_ext_ty().expect("reached maximum amount of extrinsics!");
ext_ty_map.insert("N32".into(), n32_ext_ty);
ext_ty_map.insert("F32".into(), f32_ext_ty);
ext_ty_map.insert("IO".into(), io_ext_ty);
self.n32_ext_ty = Some(n32_ext_ty);
self.io_ext_ty = Some(io_ext_ty);
enum NumericType {
F32,
N32,
}
fn ty_to_numeric_type<'ivm>(
a: ExtVal<'ivm>,
n32_ext_ty: ExtTy<'ivm>,
f32_ext_ty: ExtTy<'ivm>,
) -> Option<NumericType> {
if a.ty() == n32_ext_ty {
Some(NumericType::N32)
} else if a.ty() == f32_ext_ty {
Some(NumericType::F32)
} else {
None
}
}
fn numeric_op<'ivm>(
n32_ext_ty: ExtTy<'ivm>,
f32_ext_ty: ExtTy<'ivm>,
a: ExtVal<'ivm>,
b: ExtVal<'ivm>,
f_n32: fn(u32, u32) -> u32,
f_f32: fn(f32, f32) -> f32,
) -> ExtVal<'ivm> {
match (
ty_to_numeric_type(a, n32_ext_ty, f32_ext_ty),
ty_to_numeric_type(b, n32_ext_ty, f32_ext_ty),
) {
(Some(NumericType::N32), Some(NumericType::N32)) => {
ExtVal::new(n32_ext_ty, f_n32(a.as_ty(&n32_ext_ty), b.as_ty(&n32_ext_ty)))
}
(Some(NumericType::F32), Some(NumericType::F32)) => ExtVal::new(
f32_ext_ty,
f_f32(f32::from_bits(a.as_ty(&f32_ext_ty)), f32::from_bits(b.as_ty(&f32_ext_ty)))
.to_bits(),
),
(Some(NumericType::N32), Some(NumericType::F32)) => ExtVal::new(
f32_ext_ty,
f_f32(a.as_ty(&n32_ext_ty) as f32, f32::from_bits(b.as_ty(&f32_ext_ty))).to_bits(),
),
(Some(NumericType::F32), Some(NumericType::N32)) => ExtVal::new(
f32_ext_ty,
f_f32(f32::from_bits(a.as_ty(&f32_ext_ty)), b.as_ty(&n32_ext_ty) as f32).to_bits(),
),
_ => unimplemented!(),
}
}

fn comparison<'ivm>(
n32_ext_ty: ExtTy<'ivm>,
f32_ext_ty: ExtTy<'ivm>,
a: ExtVal<'ivm>,
b: ExtVal<'ivm>,
f_u32: fn(u32, u32) -> bool,
f_f32: fn(f32, f32) -> bool,
) -> ExtVal<'ivm> {
ExtVal::new(
n32_ext_ty,
u32::from(
match (
ty_to_numeric_type(a, n32_ext_ty, f32_ext_ty),
ty_to_numeric_type(b, n32_ext_ty, f32_ext_ty),
) {
(Some(NumericType::N32), Some(NumericType::N32)) => {
f_u32(a.as_ty(&n32_ext_ty), b.as_ty(&n32_ext_ty))
}
(Some(NumericType::F32), Some(NumericType::F32)) => {
f_f32(f32::from_bits(a.as_ty(&f32_ext_ty)), f32::from_bits(b.as_ty(&f32_ext_ty)))
}
(Some(NumericType::N32), Some(NumericType::F32)) => {
f_f32(a.as_ty(&n32_ext_ty) as f32, f32::from_bits(b.as_ty(&f32_ext_ty)))
}
(Some(NumericType::F32), Some(NumericType::N32)) => {
f_f32(f32::from_bits(a.as_ty(&f32_ext_ty)), b.as_ty(&n32_ext_ty) as f32)
}
_ => unimplemented!(),
},
),
)
}
define_ext_fns!(ext_fn_map, self,
"seq" => |a, _b| a,

"add" => |a, b| numeric_op(n32_ext_ty, f32_ext_ty, a, b, u32::wrapping_add, f32::add),
"sub" => |a, b| numeric_op(n32_ext_ty, f32_ext_ty, a, b, u32::wrapping_sub, f32::sub),
"mul" => |a, b| numeric_op(n32_ext_ty, f32_ext_ty, a, b, u32::wrapping_mul, f32::mul),
"div" => |a, b| numeric_op(n32_ext_ty, f32_ext_ty, a, b, u32::wrapping_div, f32::div),
"rem" => |a, b| numeric_op(n32_ext_ty, f32_ext_ty, a, b, u32::wrapping_rem, f32::rem_euclid),

"eq" => |a, b| comparison(n32_ext_ty, f32_ext_ty, a, b, |a, b| a == b, |a, b| a == b),
"ne" => |a, b| comparison(n32_ext_ty, f32_ext_ty, a, b, |a, b| a != b, |a, b| a != b),
"lt" => |a, b| comparison(n32_ext_ty, f32_ext_ty, a, b, |a, b| a < b, |a, b| a < b),
"le" => |a, b| comparison(n32_ext_ty, f32_ext_ty, a, b, |a, b| a <= b, |a, b| a <= b),

"n32_shl" => |a, b| ExtVal::new(n32_ext_ty, a.as_ty(&n32_ext_ty).wrapping_shl(b.as_ty(&n32_ext_ty))),
"n32_shr" => |a, b| ExtVal::new(n32_ext_ty, a.as_ty(&n32_ext_ty).wrapping_shr(b.as_ty(&n32_ext_ty))),
"n32_rotl" => |a, b| ExtVal::new(n32_ext_ty, a.as_ty(&n32_ext_ty).rotate_left(b.as_ty(&n32_ext_ty))),
"n32_rotr" => |a, b| ExtVal::new(n32_ext_ty, a.as_ty(&n32_ext_ty).rotate_right(b.as_ty(&n32_ext_ty))),

"n32_and" => |a, b| ExtVal::new(n32_ext_ty, a.as_ty(&n32_ext_ty) & b.as_ty(&n32_ext_ty)),
"n32_or" => |a, b| ExtVal::new(n32_ext_ty, a.as_ty(&n32_ext_ty) | b.as_ty(&n32_ext_ty)),
"n32_xor" => |a, b| ExtVal::new(n32_ext_ty, a.as_ty(&n32_ext_ty) ^ b.as_ty(&n32_ext_ty)),

"n32_add_high" => |a, b| ExtVal::new(n32_ext_ty, (((a.as_ty(&n32_ext_ty) as u64) + (b.as_ty(&n32_ext_ty) as u64)) >> 32) as u32),
"n32_mul_high" => |a, b| ExtVal::new(n32_ext_ty, (((a.as_ty(&n32_ext_ty) as u64) * (b.as_ty(&n32_ext_ty) as u64)) >> 32) as u32),

"io_print_char" => |a, b| {
a.as_ty(&io_ext_ty);
print!("{}", char::try_from(b.as_ty(&n32_ext_ty)).unwrap());
ExtVal::new(io_ext_ty, 0)
},
"io_print_byte" => |a, b| {
a.as_ty(&io_ext_ty);
io::stdout().write_all(&[b.as_ty(&n32_ext_ty) as u8]).unwrap();
ExtVal::new(io_ext_ty, 0)
},
"io_flush" => |a, _b| {
a.as_ty(&io_ext_ty);
io::stdout().flush().unwrap();
ExtVal::new(io_ext_ty, 0)
},
"io_read_byte" => |a, b| {
a.as_ty(&io_ext_ty);
let default = b.as_ty(&n32_ext_ty) as u8;
let mut buf = [default];
_ = io::stdin().read(&mut buf).unwrap();
ExtVal::new(n32_ext_ty, buf[0] as u32)
}
);
(ext_fn_map, ext_ty_map)
pub fn register_n32_ext_ty(&mut self) -> ExtTy<'ivm> {
let n32_ext_ty = self.register_light_ext_ty();
assert!(self.n32_ext_ty.replace(n32_ext_ty).is_none());
n32_ext_ty
}
pub fn call(
&self,
Expand Down
2 changes: 1 addition & 1 deletion ivm/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl Debug for Register {
}
}

impl<'ext, 'ivm> IVM<'ext, 'ivm> {
impl<'ivm, 'ext> IVM<'ivm, 'ext> {
/// Links the given `port` to the given `register`.
///
/// ## Safety
Expand Down
2 changes: 1 addition & 1 deletion ivm/src/interact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ macro_rules! sym {
};
}

impl<'ext, 'ivm> IVM<'ext, 'ivm> {
impl<'ivm, 'ext> IVM<'ivm, 'ext> {
/// Link two ports.
pub fn link(&mut self, a: Port<'ivm>, b: Port<'ivm>) {
use Tag::*;
Expand Down
4 changes: 2 additions & 2 deletions ivm/src/ivm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
};

/// An Interaction Virtual Machine.
pub struct IVM<'ext, 'ivm> {
pub struct IVM<'ivm, 'ext> {
/// Execution statistics of this IVM.
pub stats: Stats,

Expand All @@ -30,7 +30,7 @@ pub struct IVM<'ext, 'ivm> {
pub(crate) extrinsics: &'ext Extrinsics<'ivm>,
}

impl<'ivm, 'ext> IVM<'ext, 'ivm> {
impl<'ivm, 'ext> IVM<'ivm, 'ext> {
/// Creates a new IVM with a given heap.
pub fn new(heap: &'ivm Heap, extrinsics: &'ext Extrinsics<'ivm>) -> Self {
Self::new_from_allocator(Allocator::new(heap), extrinsics)
Expand Down
26 changes: 13 additions & 13 deletions ivm/src/parallel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{

use crate::{ivm::IVM, port::Port};

impl<'ext, 'ivm> IVM<'ext, 'ivm> {
impl<'ivm, 'ext> IVM<'ivm, 'ext> {
pub fn normalize_parallel(&mut self, threads: usize) {
self.do_fast();

Expand Down Expand Up @@ -54,8 +54,8 @@ struct Msg<'ivm> {

#[derive(Default)]
#[repr(align(256))]
struct Shared<'ext, 'ivm> {
ivm_return: OnceLock<IVM<'ext, 'ivm>>,
struct Shared<'ivm, 'ext> {
ivm_return: OnceLock<IVM<'ivm, 'ext>>,
msg: Mutex<Msg<'ivm>>,
condvar: Condvar,
}
Expand All @@ -72,15 +72,15 @@ enum MsgKind {
Exit,
}

struct Worker<'w, 'ext, 'ivm> {
ivm: IVM<'ext, 'ivm>,
shared: &'w Shared<'ext, 'ivm>,
struct Worker<'w, 'ivm, 'ext> {
ivm: IVM<'ivm, 'ext>,
shared: &'w Shared<'ivm, 'ext>,
dispatch: Thread,
}

const ERA_LENGTH: u32 = 512;

impl<'w, 'ext, 'ivm> Worker<'w, 'ext, 'ivm> {
impl<'w, 'ivm, 'ext> Worker<'w, 'ivm, 'ext> {
fn execute(mut self) {
self.work();
self.shared.ivm_return.set(self.ivm).ok().unwrap();
Expand Down Expand Up @@ -142,16 +142,16 @@ impl<'w, 'ext, 'ivm> Worker<'w, 'ext, 'ivm> {
}
}

struct WorkerHandle<'w, 'ext, 'ivm> {
shared: &'w Shared<'ext, 'ivm>,
struct WorkerHandle<'w, 'ivm, 'ext> {
shared: &'w Shared<'ivm, 'ext>,
}

struct Dispatch<'w, 'ext, 'ivm> {
active: Vec<WorkerHandle<'w, 'ext, 'ivm>>,
idle: Vec<WorkerHandle<'w, 'ext, 'ivm>>,
struct Dispatch<'w, 'ivm, 'ext> {
active: Vec<WorkerHandle<'w, 'ivm, 'ext>>,
idle: Vec<WorkerHandle<'w, 'ivm, 'ext>>,
}

impl<'w, 'ext, 'ivm> Dispatch<'w, 'ext, 'ivm> {
impl<'w, 'ivm, 'ext> Dispatch<'w, 'ivm, 'ext> {
fn execute(mut self) {
loop {
let mut i = 0;
Expand Down
Loading

0 comments on commit 20a7ab9

Please sign in to comment.