chore(fmt): add legacy stubs and strip trailing whitespace to unblock cargo fmt

This commit is contained in:
Selfhosting Dev
2025-09-17 07:43:07 +09:00
parent fcf8ce1f3c
commit adbb0201a9
385 changed files with 35622 additions and 15004 deletions

View File

@ -1,6 +1,6 @@
use super::Semantics;
use crate::jit::lower::builder::{IRBuilder, BinOpKind, CmpKind};
use crate::mir::{ValueId, BasicBlockId};
use crate::jit::lower::builder::{BinOpKind, CmpKind, IRBuilder};
use crate::mir::{BasicBlockId, ValueId};
/// Adapter that translates Semantics operations into IRBuilder calls (Cranelift path)
pub struct ClifSemanticsAdapter<'a> {
@ -8,7 +8,9 @@ pub struct ClifSemanticsAdapter<'a> {
}
impl<'a> ClifSemanticsAdapter<'a> {
pub fn new(builder: &'a mut dyn IRBuilder) -> Self { Self { builder } }
pub fn new(builder: &'a mut dyn IRBuilder) -> Self {
Self { builder }
}
}
impl<'a> Semantics for ClifSemanticsAdapter<'a> {
@ -16,40 +18,107 @@ impl<'a> Semantics for ClifSemanticsAdapter<'a> {
type Ptr = ValueId;
type BB = BasicBlockId;
fn const_i64(&mut self, v: i64) -> Self::Val { self.builder.emit_const_i64(v); }
fn const_f64(&mut self, v: f64) -> Self::Val { self.builder.emit_const_f64(v); }
fn const_bool(&mut self, v: bool) -> Self::Val { self.builder.emit_const_i64(if v {1} else {0}); }
fn const_null(&mut self) -> Self::Val { self.builder.emit_const_i64(0); }
fn const_str(&mut self, _s: &str) -> Self::Val { self.builder.emit_const_i64(0); }
fn const_i64(&mut self, v: i64) -> Self::Val {
self.builder.emit_const_i64(v);
}
fn const_f64(&mut self, v: f64) -> Self::Val {
self.builder.emit_const_f64(v);
}
fn const_bool(&mut self, v: bool) -> Self::Val {
self.builder.emit_const_i64(if v { 1 } else { 0 });
}
fn const_null(&mut self) -> Self::Val {
self.builder.emit_const_i64(0);
}
fn const_str(&mut self, _s: &str) -> Self::Val {
self.builder.emit_const_i64(0);
}
fn neg(&mut self, _x: Self::Val) -> Self::Val { self.builder.emit_binop(BinOpKind::Sub); }
fn not(&mut self, _x: Self::Val) -> Self::Val { /* handled via compare/select in LowerCore */ }
fn bit_not(&mut self, _x: Self::Val) -> Self::Val { /* not used here */ }
fn add(&mut self, _a: Self::Val, _b: Self::Val) -> Self::Val { self.builder.emit_binop(BinOpKind::Add); }
fn sub(&mut self, _a: Self::Val, _b: Self::Val) -> Self::Val { self.builder.emit_binop(BinOpKind::Sub); }
fn mul(&mut self, _a: Self::Val, _b: Self::Val) -> Self::Val { self.builder.emit_binop(BinOpKind::Mul); }
fn div(&mut self, _a: Self::Val, _b: Self::Val) -> Self::Val { self.builder.emit_binop(BinOpKind::Div); }
fn modulo(&mut self, _a: Self::Val, _b: Self::Val) -> Self::Val { self.builder.emit_binop(BinOpKind::Mod); }
fn cmp_eq(&mut self, _a: Self::Val, _b: Self::Val) -> Self::Val { self.builder.emit_compare(CmpKind::Eq); }
fn cmp_ne(&mut self, _a: Self::Val, _b: Self::Val) -> Self::Val { self.builder.emit_compare(CmpKind::Ne); }
fn cmp_lt(&mut self, _a: Self::Val, _b: Self::Val) -> Self::Val { self.builder.emit_compare(CmpKind::Lt); }
fn cmp_le(&mut self, _a: Self::Val, _b: Self::Val) -> Self::Val { self.builder.emit_compare(CmpKind::Le); }
fn cmp_gt(&mut self, _a: Self::Val, _b: Self::Val) -> Self::Val { self.builder.emit_compare(CmpKind::Gt); }
fn cmp_ge(&mut self, _a: Self::Val, _b: Self::Val) -> Self::Val { self.builder.emit_compare(CmpKind::Ge); }
fn neg(&mut self, _x: Self::Val) -> Self::Val {
self.builder.emit_binop(BinOpKind::Sub);
}
fn not(&mut self, _x: Self::Val) -> Self::Val { /* handled via compare/select in LowerCore */
}
fn bit_not(&mut self, _x: Self::Val) -> Self::Val { /* not used here */
}
fn add(&mut self, _a: Self::Val, _b: Self::Val) -> Self::Val {
self.builder.emit_binop(BinOpKind::Add);
}
fn sub(&mut self, _a: Self::Val, _b: Self::Val) -> Self::Val {
self.builder.emit_binop(BinOpKind::Sub);
}
fn mul(&mut self, _a: Self::Val, _b: Self::Val) -> Self::Val {
self.builder.emit_binop(BinOpKind::Mul);
}
fn div(&mut self, _a: Self::Val, _b: Self::Val) -> Self::Val {
self.builder.emit_binop(BinOpKind::Div);
}
fn modulo(&mut self, _a: Self::Val, _b: Self::Val) -> Self::Val {
self.builder.emit_binop(BinOpKind::Mod);
}
fn cmp_eq(&mut self, _a: Self::Val, _b: Self::Val) -> Self::Val {
self.builder.emit_compare(CmpKind::Eq);
}
fn cmp_ne(&mut self, _a: Self::Val, _b: Self::Val) -> Self::Val {
self.builder.emit_compare(CmpKind::Ne);
}
fn cmp_lt(&mut self, _a: Self::Val, _b: Self::Val) -> Self::Val {
self.builder.emit_compare(CmpKind::Lt);
}
fn cmp_le(&mut self, _a: Self::Val, _b: Self::Val) -> Self::Val {
self.builder.emit_compare(CmpKind::Le);
}
fn cmp_gt(&mut self, _a: Self::Val, _b: Self::Val) -> Self::Val {
self.builder.emit_compare(CmpKind::Gt);
}
fn cmp_ge(&mut self, _a: Self::Val, _b: Self::Val) -> Self::Val {
self.builder.emit_compare(CmpKind::Ge);
}
fn alloca_ptr(&mut self, _vid: ValueId) -> Self::Ptr { _vid }
fn load(&mut self, _ptr: &Self::Ptr) -> Self::Val { self.builder.load_local_i64(_ptr.as_u32() as usize); }
fn store(&mut self, _ptr: &Self::Ptr, _v: Self::Val) { self.builder.store_local_i64(_ptr.as_u32() as usize); }
fn jump(&mut self, _target: BasicBlockId) { /* handled by LowerCore */ }
fn branch(&mut self, _cond: Self::Val, _then_bb: BasicBlockId, _else_bb: BasicBlockId) { /* handled by LowerCore */ }
fn phi_select(&mut self, _incoming: &[(BasicBlockId, Self::Val)]) -> Self::Val { () }
fn ret(&mut self, _v: Option<Self::Val>) { self.builder.emit_return(); }
fn alloca_ptr(&mut self, _vid: ValueId) -> Self::Ptr {
_vid
}
fn load(&mut self, _ptr: &Self::Ptr) -> Self::Val {
self.builder.load_local_i64(_ptr.as_u32() as usize);
}
fn store(&mut self, _ptr: &Self::Ptr, _v: Self::Val) {
self.builder.store_local_i64(_ptr.as_u32() as usize);
}
fn jump(&mut self, _target: BasicBlockId) { /* handled by LowerCore */
}
fn branch(&mut self, _cond: Self::Val, _then_bb: BasicBlockId, _else_bb: BasicBlockId) {
/* handled by LowerCore */
}
fn phi_select(&mut self, _incoming: &[(BasicBlockId, Self::Val)]) -> Self::Val {
()
}
fn ret(&mut self, _v: Option<Self::Val>) {
self.builder.emit_return();
}
fn new_box(&mut self, _type_id: i64, _args: &[Self::Val]) -> Self::Val { () }
fn box_call_tagged(&mut self, _type_id: i64, _method_id: i64, _recv: Self::Val, _argv: &[Self::Val], _tags: &[i64]) -> Self::Val { () }
fn extern_call(&mut self, _iface: &str, _method: &str, _args: &[Self::Val]) -> Self::Val { () }
fn new_box(&mut self, _type_id: i64, _args: &[Self::Val]) -> Self::Val {
()
}
fn box_call_tagged(
&mut self,
_type_id: i64,
_method_id: i64,
_recv: Self::Val,
_argv: &[Self::Val],
_tags: &[i64],
) -> Self::Val {
()
}
fn extern_call(&mut self, _iface: &str, _method: &str, _args: &[Self::Val]) -> Self::Val {
()
}
fn barrier_read(&mut self, v: Self::Val) -> Self::Val { v }
fn barrier_write(&mut self, _ptr: &Self::Ptr, v: Self::Val) -> Self::Val { v }
fn safepoint(&mut self) { /* Lowered via explicit hostcall in LowerCore path */ }
fn barrier_read(&mut self, v: Self::Val) -> Self::Val {
v
}
fn barrier_write(&mut self, _ptr: &Self::Ptr, v: Self::Val) -> Self::Val {
v
}
fn safepoint(&mut self) { /* Lowered via explicit hostcall in LowerCore path */
}
}

View File

@ -9,7 +9,7 @@
#![allow(dead_code)]
use crate::mir::{ValueId, BasicBlockId};
use crate::mir::{BasicBlockId, ValueId};
/// The unified semantics interface for MIR evaluation/lowering.
pub trait Semantics {
@ -55,21 +55,34 @@ pub trait Semantics {
// Host/Box calls
fn new_box(&mut self, type_id: i64, args: &[Self::Val]) -> Self::Val;
fn box_call_tagged(&mut self, type_id: i64, method_id: i64, recv: Self::Val, argv: &[Self::Val], tags: &[i64]) -> Self::Val;
fn box_call_tagged(
&mut self,
type_id: i64,
method_id: i64,
recv: Self::Val,
argv: &[Self::Val],
tags: &[i64],
) -> Self::Val;
fn extern_call(&mut self, iface: &str, method: &str, args: &[Self::Val]) -> Self::Val;
// GC hooks
fn barrier_read(&mut self, v: Self::Val) -> Self::Val { v }
fn barrier_write(&mut self, _ptr: &Self::Ptr, v: Self::Val) -> Self::Val { v }
fn barrier_read(&mut self, v: Self::Val) -> Self::Val {
v
}
fn barrier_write(&mut self, _ptr: &Self::Ptr, v: Self::Val) -> Self::Val {
v
}
fn safepoint(&mut self) {}
}
/// Optional helpers extension — default blanket impl with conveniences.
pub trait SemanticsExt: Semantics {
fn to_bool_hint(&mut self, v: Self::Val) -> Self::Val { v }
fn to_bool_hint(&mut self, v: Self::Val) -> Self::Val {
v
}
}
impl<T: Semantics> SemanticsExt for T {}
pub mod vm_impl;
pub mod clif_adapter;
pub mod vm_impl;

View File

@ -1,7 +1,7 @@
use std::collections::HashMap;
use crate::mir::{ValueId, BasicBlockId};
use crate::backend::vm::VMValue;
use crate::mir::{BasicBlockId, ValueId};
use super::Semantics;
@ -13,7 +13,12 @@ pub struct VmSemantics {
}
impl VmSemantics {
pub fn new() -> Self { Self { vals: HashMap::new(), last_ret: None } }
pub fn new() -> Self {
Self {
vals: HashMap::new(),
last_ret: None,
}
}
}
impl Semantics for VmSemantics {
@ -21,61 +26,146 @@ impl Semantics for VmSemantics {
type Ptr = ValueId; // address by MIR value id (local slot semantics)
type BB = BasicBlockId;
fn const_i64(&mut self, v: i64) -> Self::Val { VMValue::Integer(v) }
fn const_f64(&mut self, v: f64) -> Self::Val { VMValue::Float(v) }
fn const_bool(&mut self, v: bool) -> Self::Val { VMValue::Bool(v) }
fn const_null(&mut self) -> Self::Val { VMValue::Void }
fn const_str(&mut self, s: &str) -> Self::Val { VMValue::String(s.to_string()) }
fn const_i64(&mut self, v: i64) -> Self::Val {
VMValue::Integer(v)
}
fn const_f64(&mut self, v: f64) -> Self::Val {
VMValue::Float(v)
}
fn const_bool(&mut self, v: bool) -> Self::Val {
VMValue::Bool(v)
}
fn const_null(&mut self) -> Self::Val {
VMValue::Void
}
fn const_str(&mut self, s: &str) -> Self::Val {
VMValue::String(s.to_string())
}
fn neg(&mut self, x: Self::Val) -> Self::Val {
match x { VMValue::Integer(i) => VMValue::Integer(-i), VMValue::Float(f) => VMValue::Float(-f), _ => VMValue::Integer(0) }
match x {
VMValue::Integer(i) => VMValue::Integer(-i),
VMValue::Float(f) => VMValue::Float(-f),
_ => VMValue::Integer(0),
}
}
fn not(&mut self, x: Self::Val) -> Self::Val {
match x { VMValue::Bool(b) => VMValue::Bool(!b), VMValue::Integer(i) => VMValue::Bool(i==0), _ => VMValue::Bool(false) }
match x {
VMValue::Bool(b) => VMValue::Bool(!b),
VMValue::Integer(i) => VMValue::Bool(i == 0),
_ => VMValue::Bool(false),
}
}
fn bit_not(&mut self, x: Self::Val) -> Self::Val {
match x { VMValue::Integer(i) => VMValue::Integer(!i), _ => VMValue::Integer(0) }
match x {
VMValue::Integer(i) => VMValue::Integer(!i),
_ => VMValue::Integer(0),
}
}
fn add(&mut self, a: Self::Val, b: Self::Val) -> Self::Val {
match (a,b) { (VMValue::Integer(x), VMValue::Integer(y)) => VMValue::Integer(x+y), (VMValue::Float(x), VMValue::Float(y)) => VMValue::Float(x+y), _ => VMValue::Integer(0) }
match (a, b) {
(VMValue::Integer(x), VMValue::Integer(y)) => VMValue::Integer(x + y),
(VMValue::Float(x), VMValue::Float(y)) => VMValue::Float(x + y),
_ => VMValue::Integer(0),
}
}
fn sub(&mut self, a: Self::Val, b: Self::Val) -> Self::Val {
match (a,b) { (VMValue::Integer(x), VMValue::Integer(y)) => VMValue::Integer(x-y), (VMValue::Float(x), VMValue::Float(y)) => VMValue::Float(x-y), _ => VMValue::Integer(0) }
match (a, b) {
(VMValue::Integer(x), VMValue::Integer(y)) => VMValue::Integer(x - y),
(VMValue::Float(x), VMValue::Float(y)) => VMValue::Float(x - y),
_ => VMValue::Integer(0),
}
}
fn mul(&mut self, a: Self::Val, b: Self::Val) -> Self::Val {
match (a,b) { (VMValue::Integer(x), VMValue::Integer(y)) => VMValue::Integer(x*y), (VMValue::Float(x), VMValue::Float(y)) => VMValue::Float(x*y), _ => VMValue::Integer(0) }
match (a, b) {
(VMValue::Integer(x), VMValue::Integer(y)) => VMValue::Integer(x * y),
(VMValue::Float(x), VMValue::Float(y)) => VMValue::Float(x * y),
_ => VMValue::Integer(0),
}
}
fn div(&mut self, a: Self::Val, b: Self::Val) -> Self::Val {
match (a,b) { (VMValue::Integer(x), VMValue::Integer(y)) if y!=0 => VMValue::Integer(x/y), (VMValue::Float(x), VMValue::Float(y)) => VMValue::Float(x/y), _ => VMValue::Integer(0) }
match (a, b) {
(VMValue::Integer(x), VMValue::Integer(y)) if y != 0 => VMValue::Integer(x / y),
(VMValue::Float(x), VMValue::Float(y)) => VMValue::Float(x / y),
_ => VMValue::Integer(0),
}
}
fn modulo(&mut self, a: Self::Val, b: Self::Val) -> Self::Val {
match (a,b) { (VMValue::Integer(x), VMValue::Integer(y)) if y!=0 => VMValue::Integer(x%y), _ => VMValue::Integer(0) }
match (a, b) {
(VMValue::Integer(x), VMValue::Integer(y)) if y != 0 => VMValue::Integer(x % y),
_ => VMValue::Integer(0),
}
}
fn cmp_eq(&mut self, a: Self::Val, b: Self::Val) -> Self::Val {
VMValue::Bool(a == b)
}
fn cmp_ne(&mut self, a: Self::Val, b: Self::Val) -> Self::Val {
VMValue::Bool(a != b)
}
fn cmp_eq(&mut self, a: Self::Val, b: Self::Val) -> Self::Val { VMValue::Bool(a==b) }
fn cmp_ne(&mut self, a: Self::Val, b: Self::Val) -> Self::Val { VMValue::Bool(a!=b) }
fn cmp_lt(&mut self, a: Self::Val, b: Self::Val) -> Self::Val {
match (a,b) { (VMValue::Integer(x), VMValue::Integer(y)) => VMValue::Bool(x<y), (VMValue::Float(x), VMValue::Float(y)) => VMValue::Bool(x<y), _ => VMValue::Bool(false) }
match (a, b) {
(VMValue::Integer(x), VMValue::Integer(y)) => VMValue::Bool(x < y),
(VMValue::Float(x), VMValue::Float(y)) => VMValue::Bool(x < y),
_ => VMValue::Bool(false),
}
}
fn cmp_le(&mut self, a: Self::Val, b: Self::Val) -> Self::Val {
match (a,b) { (VMValue::Integer(x), VMValue::Integer(y)) => VMValue::Bool(x<=y), (VMValue::Float(x), VMValue::Float(y)) => VMValue::Bool(x<=y), _ => VMValue::Bool(false) }
match (a, b) {
(VMValue::Integer(x), VMValue::Integer(y)) => VMValue::Bool(x <= y),
(VMValue::Float(x), VMValue::Float(y)) => VMValue::Bool(x <= y),
_ => VMValue::Bool(false),
}
}
fn cmp_gt(&mut self, a: Self::Val, b: Self::Val) -> Self::Val {
match (a,b) { (VMValue::Integer(x), VMValue::Integer(y)) => VMValue::Bool(x>y), (VMValue::Float(x), VMValue::Float(y)) => VMValue::Bool(x>y), _ => VMValue::Bool(false) }
match (a, b) {
(VMValue::Integer(x), VMValue::Integer(y)) => VMValue::Bool(x > y),
(VMValue::Float(x), VMValue::Float(y)) => VMValue::Bool(x > y),
_ => VMValue::Bool(false),
}
}
fn cmp_ge(&mut self, a: Self::Val, b: Self::Val) -> Self::Val {
match (a,b) { (VMValue::Integer(x), VMValue::Integer(y)) => VMValue::Bool(x>=y), (VMValue::Float(x), VMValue::Float(y)) => VMValue::Bool(x>=y), _ => VMValue::Bool(false) }
match (a, b) {
(VMValue::Integer(x), VMValue::Integer(y)) => VMValue::Bool(x >= y),
(VMValue::Float(x), VMValue::Float(y)) => VMValue::Bool(x >= y),
_ => VMValue::Bool(false),
}
}
fn alloca_ptr(&mut self, vid: ValueId) -> Self::Ptr { vid }
fn load(&mut self, ptr: &Self::Ptr) -> Self::Val { self.vals.get(ptr).cloned().unwrap_or(VMValue::Integer(0)) }
fn store(&mut self, ptr: &Self::Ptr, v: Self::Val) { self.vals.insert(*ptr, v); }
fn alloca_ptr(&mut self, vid: ValueId) -> Self::Ptr {
vid
}
fn load(&mut self, ptr: &Self::Ptr) -> Self::Val {
self.vals.get(ptr).cloned().unwrap_or(VMValue::Integer(0))
}
fn store(&mut self, ptr: &Self::Ptr, v: Self::Val) {
self.vals.insert(*ptr, v);
}
fn jump(&mut self, _target: BasicBlockId) {}
fn branch(&mut self, _cond: Self::Val, _then_bb: BasicBlockId, _else_bb: BasicBlockId) {}
fn phi_select(&mut self, incoming: &[(BasicBlockId, Self::Val)]) -> Self::Val { incoming.last().map(|(_,v)| v.clone()).unwrap_or(VMValue::Integer(0)) }
fn ret(&mut self, v: Option<Self::Val>) { self.last_ret = v; }
fn phi_select(&mut self, incoming: &[(BasicBlockId, Self::Val)]) -> Self::Val {
incoming
.last()
.map(|(_, v)| v.clone())
.unwrap_or(VMValue::Integer(0))
}
fn ret(&mut self, v: Option<Self::Val>) {
self.last_ret = v;
}
fn new_box(&mut self, _type_id: i64, _args: &[Self::Val]) -> Self::Val { VMValue::Integer(0) }
fn box_call_tagged(&mut self, _type_id: i64, _method_id: i64, _recv: Self::Val, _argv: &[Self::Val], _tags: &[i64]) -> Self::Val { VMValue::Integer(0) }
fn extern_call(&mut self, _iface: &str, _method: &str, _args: &[Self::Val]) -> Self::Val { VMValue::Integer(0) }
fn new_box(&mut self, _type_id: i64, _args: &[Self::Val]) -> Self::Val {
VMValue::Integer(0)
}
fn box_call_tagged(
&mut self,
_type_id: i64,
_method_id: i64,
_recv: Self::Val,
_argv: &[Self::Val],
_tags: &[i64],
) -> Self::Val {
VMValue::Integer(0)
}
fn extern_call(&mut self, _iface: &str, _method: &str, _args: &[Self::Val]) -> Self::Val {
VMValue::Integer(0)
}
}