intrinsics pr-01
This commit is contained in:
@@ -0,0 +1,462 @@
|
||||
use prometeu_bytecode::Value;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct BuiltinTypeKey {
|
||||
pub name: &'static str,
|
||||
pub version: u16,
|
||||
}
|
||||
|
||||
impl BuiltinTypeKey {
|
||||
pub const fn new(name: &'static str, version: u16) -> Self {
|
||||
Self { name, version }
|
||||
}
|
||||
|
||||
pub const fn key(self) -> (&'static str, u16) {
|
||||
(self.name, self.version)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct BuiltinConstKey {
|
||||
pub target: &'static str,
|
||||
pub name: &'static str,
|
||||
pub version: u16,
|
||||
}
|
||||
|
||||
impl BuiltinConstKey {
|
||||
pub const fn new(target: &'static str, name: &'static str, version: u16) -> Self {
|
||||
Self { target, name, version }
|
||||
}
|
||||
|
||||
pub const fn key(self) -> (&'static str, &'static str, u16) {
|
||||
(self.target, self.name, self.version)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct IntrinsicKey {
|
||||
pub owner: &'static str,
|
||||
pub name: &'static str,
|
||||
pub version: u16,
|
||||
}
|
||||
|
||||
impl IntrinsicKey {
|
||||
pub const fn new(owner: &'static str, name: &'static str, version: u16) -> Self {
|
||||
Self { owner, name, version }
|
||||
}
|
||||
|
||||
pub const fn key(self) -> (&'static str, &'static str, u16) {
|
||||
(self.owner, self.name, self.version)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum BuiltinScalarType {
|
||||
Int,
|
||||
Float,
|
||||
Bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum AbiType {
|
||||
Scalar(BuiltinScalarType),
|
||||
Builtin(BuiltinTypeKey),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum BuiltinLayoutType {
|
||||
Scalar(BuiltinScalarType),
|
||||
Builtin(BuiltinTypeKey),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum BuiltinTypeShape {
|
||||
Scalar,
|
||||
Aggregate,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct BuiltinFieldMeta {
|
||||
pub name: &'static str,
|
||||
pub start_slot: u16,
|
||||
pub field_type: BuiltinLayoutType,
|
||||
pub flat_slot_width: u16,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct BuiltinTypeMeta {
|
||||
pub id: u32,
|
||||
pub name: &'static str,
|
||||
pub version: u16,
|
||||
pub shape: BuiltinTypeShape,
|
||||
pub fields: &'static [BuiltinFieldMeta],
|
||||
pub flat_slot_layout: &'static [AbiType],
|
||||
pub flat_slot_width: u16,
|
||||
}
|
||||
|
||||
impl BuiltinTypeMeta {
|
||||
pub const fn key(&self) -> BuiltinTypeKey {
|
||||
BuiltinTypeKey::new(self.name, self.version)
|
||||
}
|
||||
|
||||
pub fn field(&self, name: &str) -> Option<&'static BuiltinFieldMeta> {
|
||||
self.fields.iter().find(|field| field.name == name)
|
||||
}
|
||||
|
||||
pub fn flatten_layout(&self) -> Vec<AbiType> {
|
||||
let mut out = Vec::with_capacity(self.flat_slot_width as usize);
|
||||
self.flatten_into(&mut out);
|
||||
out
|
||||
}
|
||||
|
||||
fn flatten_into(&self, out: &mut Vec<AbiType>) {
|
||||
match self.shape {
|
||||
BuiltinTypeShape::Scalar => out.extend_from_slice(self.flat_slot_layout),
|
||||
BuiltinTypeShape::Aggregate => {
|
||||
for field in self.fields {
|
||||
flatten_layout_type(field.field_type, out);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub enum BuiltinConstSlotValue {
|
||||
Int32(i32),
|
||||
Int64(i64),
|
||||
Float(f64),
|
||||
Bool(bool),
|
||||
NominalBuiltin { builtin: BuiltinTypeKey, carrier: i32 },
|
||||
}
|
||||
|
||||
pub type BuiltinConstHook = fn() -> Vec<Value>;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum BuiltinConstMaterializer {
|
||||
Direct(&'static [BuiltinConstSlotValue]),
|
||||
Hook(BuiltinConstHook),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct BuiltinConstMeta {
|
||||
pub key: BuiltinConstKey,
|
||||
pub flat_slot_layout: &'static [AbiType],
|
||||
pub flat_slot_width: u16,
|
||||
pub materializer: BuiltinConstMaterializer,
|
||||
}
|
||||
|
||||
impl BuiltinConstMeta {
|
||||
pub fn direct_slots(&self) -> Option<&'static [BuiltinConstSlotValue]> {
|
||||
match self.materializer {
|
||||
BuiltinConstMaterializer::Direct(slots) => Some(slots),
|
||||
BuiltinConstMaterializer::Hook(_) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum IntrinsicExecutionError {
|
||||
ArityMismatch { expected: usize, got: usize },
|
||||
TypeMismatch { index: usize, expected: AbiType },
|
||||
}
|
||||
|
||||
pub type IntrinsicImplementation = fn(&[Value]) -> Result<Vec<Value>, IntrinsicExecutionError>;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct IntrinsicMeta {
|
||||
pub id: u32,
|
||||
pub owner: &'static str,
|
||||
pub name: &'static str,
|
||||
pub version: u16,
|
||||
pub arg_layout: &'static [AbiType],
|
||||
pub ret_layout: &'static [AbiType],
|
||||
pub deterministic: bool,
|
||||
pub may_allocate: bool,
|
||||
pub implementation: IntrinsicImplementation,
|
||||
}
|
||||
|
||||
impl IntrinsicMeta {
|
||||
pub const fn key(&self) -> IntrinsicKey {
|
||||
IntrinsicKey::new(self.owner, self.name, self.version)
|
||||
}
|
||||
}
|
||||
|
||||
const COLOR: BuiltinTypeKey = BuiltinTypeKey::new("color", 1);
|
||||
const VEC2: BuiltinTypeKey = BuiltinTypeKey::new("vec2", 1);
|
||||
const PIXEL: BuiltinTypeKey = BuiltinTypeKey::new("pixel", 1);
|
||||
|
||||
const COLOR_LAYOUT: [AbiType; 1] = [AbiType::Builtin(COLOR)];
|
||||
const VEC2_LAYOUT: [AbiType; 2] =
|
||||
[AbiType::Scalar(BuiltinScalarType::Float), AbiType::Scalar(BuiltinScalarType::Float)];
|
||||
const PIXEL_LAYOUT: [AbiType; 3] = [
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Builtin(COLOR),
|
||||
];
|
||||
|
||||
const VEC2_FIELDS: [BuiltinFieldMeta; 2] = [
|
||||
BuiltinFieldMeta {
|
||||
name: "x",
|
||||
start_slot: 0,
|
||||
field_type: BuiltinLayoutType::Scalar(BuiltinScalarType::Float),
|
||||
flat_slot_width: 1,
|
||||
},
|
||||
BuiltinFieldMeta {
|
||||
name: "y",
|
||||
start_slot: 1,
|
||||
field_type: BuiltinLayoutType::Scalar(BuiltinScalarType::Float),
|
||||
flat_slot_width: 1,
|
||||
},
|
||||
];
|
||||
|
||||
const PIXEL_FIELDS: [BuiltinFieldMeta; 3] = [
|
||||
BuiltinFieldMeta {
|
||||
name: "x",
|
||||
start_slot: 0,
|
||||
field_type: BuiltinLayoutType::Scalar(BuiltinScalarType::Int),
|
||||
flat_slot_width: 1,
|
||||
},
|
||||
BuiltinFieldMeta {
|
||||
name: "y",
|
||||
start_slot: 1,
|
||||
field_type: BuiltinLayoutType::Scalar(BuiltinScalarType::Int),
|
||||
flat_slot_width: 1,
|
||||
},
|
||||
BuiltinFieldMeta {
|
||||
name: "color",
|
||||
start_slot: 2,
|
||||
field_type: BuiltinLayoutType::Builtin(COLOR),
|
||||
flat_slot_width: 1,
|
||||
},
|
||||
];
|
||||
|
||||
const BUILTIN_TYPES: [BuiltinTypeMeta; 3] = [
|
||||
BuiltinTypeMeta {
|
||||
id: 0x0001,
|
||||
name: COLOR.name,
|
||||
version: COLOR.version,
|
||||
shape: BuiltinTypeShape::Scalar,
|
||||
fields: &[],
|
||||
flat_slot_layout: &COLOR_LAYOUT,
|
||||
flat_slot_width: 1,
|
||||
},
|
||||
BuiltinTypeMeta {
|
||||
id: 0x0002,
|
||||
name: VEC2.name,
|
||||
version: VEC2.version,
|
||||
shape: BuiltinTypeShape::Aggregate,
|
||||
fields: &VEC2_FIELDS,
|
||||
flat_slot_layout: &VEC2_LAYOUT,
|
||||
flat_slot_width: 2,
|
||||
},
|
||||
BuiltinTypeMeta {
|
||||
id: 0x0003,
|
||||
name: PIXEL.name,
|
||||
version: PIXEL.version,
|
||||
shape: BuiltinTypeShape::Aggregate,
|
||||
fields: &PIXEL_FIELDS,
|
||||
flat_slot_layout: &PIXEL_LAYOUT,
|
||||
flat_slot_width: 3,
|
||||
},
|
||||
];
|
||||
|
||||
const VEC2_ZERO_LAYOUT: [AbiType; 2] =
|
||||
[AbiType::Scalar(BuiltinScalarType::Float), AbiType::Scalar(BuiltinScalarType::Float)];
|
||||
const VEC2_ZERO_SLOTS: [BuiltinConstSlotValue; 2] =
|
||||
[BuiltinConstSlotValue::Float(0.0), BuiltinConstSlotValue::Float(0.0)];
|
||||
const BUILTIN_CONSTS: [BuiltinConstMeta; 1] = [BuiltinConstMeta {
|
||||
key: BuiltinConstKey::new("vec2", "zero", 1),
|
||||
flat_slot_layout: &VEC2_ZERO_LAYOUT,
|
||||
flat_slot_width: 2,
|
||||
materializer: BuiltinConstMaterializer::Direct(&VEC2_ZERO_SLOTS),
|
||||
}];
|
||||
|
||||
const VEC2_DOT_ARGS: [AbiType; 4] = [
|
||||
AbiType::Scalar(BuiltinScalarType::Float),
|
||||
AbiType::Scalar(BuiltinScalarType::Float),
|
||||
AbiType::Scalar(BuiltinScalarType::Float),
|
||||
AbiType::Scalar(BuiltinScalarType::Float),
|
||||
];
|
||||
const SINGLE_FLOAT_RET: [AbiType; 1] = [AbiType::Scalar(BuiltinScalarType::Float)];
|
||||
const VEC2_LENGTH_ARGS: [AbiType; 2] =
|
||||
[AbiType::Scalar(BuiltinScalarType::Float), AbiType::Scalar(BuiltinScalarType::Float)];
|
||||
const INTRINSICS: [IntrinsicMeta; 2] = [
|
||||
IntrinsicMeta {
|
||||
id: 0x1000,
|
||||
owner: "vec2",
|
||||
name: "dot",
|
||||
version: 1,
|
||||
arg_layout: &VEC2_DOT_ARGS,
|
||||
ret_layout: &SINGLE_FLOAT_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: vec2_dot,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x1001,
|
||||
owner: "vec2",
|
||||
name: "length",
|
||||
version: 1,
|
||||
arg_layout: &VEC2_LENGTH_ARGS,
|
||||
ret_layout: &SINGLE_FLOAT_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: vec2_length,
|
||||
},
|
||||
];
|
||||
|
||||
pub fn lookup_builtin_type(name: &str, version: u16) -> Option<&'static BuiltinTypeMeta> {
|
||||
BUILTIN_TYPES.iter().find(|meta| meta.name == name && meta.version == version)
|
||||
}
|
||||
|
||||
pub fn lookup_builtin_constant(
|
||||
target: &str,
|
||||
name: &str,
|
||||
version: u16,
|
||||
) -> Option<&'static BuiltinConstMeta> {
|
||||
BUILTIN_CONSTS.iter().find(|meta| {
|
||||
meta.key.target == target && meta.key.name == name && meta.key.version == version
|
||||
})
|
||||
}
|
||||
|
||||
pub fn lookup_intrinsic(owner: &str, name: &str, version: u16) -> Option<&'static IntrinsicMeta> {
|
||||
INTRINSICS
|
||||
.iter()
|
||||
.find(|meta| meta.owner == owner && meta.name == name && meta.version == version)
|
||||
}
|
||||
|
||||
pub fn lookup_intrinsic_by_id(id: u32) -> Option<&'static IntrinsicMeta> {
|
||||
INTRINSICS.iter().find(|meta| meta.id == id)
|
||||
}
|
||||
|
||||
fn flatten_layout_type(layout_type: BuiltinLayoutType, out: &mut Vec<AbiType>) {
|
||||
match layout_type {
|
||||
BuiltinLayoutType::Scalar(scalar) => out.push(AbiType::Scalar(scalar)),
|
||||
BuiltinLayoutType::Builtin(key) => {
|
||||
let meta = lookup_builtin_type(key.name, key.version).unwrap_or_else(|| {
|
||||
panic!("missing builtin metadata for {}@{}", key.name, key.version)
|
||||
});
|
||||
meta.flatten_into(out);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn expect_float_arg(args: &[Value], index: usize) -> Result<f64, IntrinsicExecutionError> {
|
||||
let value = args
|
||||
.get(index)
|
||||
.ok_or(IntrinsicExecutionError::ArityMismatch { expected: index + 1, got: args.len() })?;
|
||||
value.as_float().ok_or(IntrinsicExecutionError::TypeMismatch {
|
||||
index,
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Float),
|
||||
})
|
||||
}
|
||||
|
||||
fn vec2_dot(args: &[Value]) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
if args.len() != 4 {
|
||||
return Err(IntrinsicExecutionError::ArityMismatch { expected: 4, got: args.len() });
|
||||
}
|
||||
let ax = expect_float_arg(args, 0)?;
|
||||
let ay = expect_float_arg(args, 1)?;
|
||||
let bx = expect_float_arg(args, 2)?;
|
||||
let by = expect_float_arg(args, 3)?;
|
||||
Ok(vec![Value::Float((ax * bx) + (ay * by))])
|
||||
}
|
||||
|
||||
fn vec2_length(args: &[Value]) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
if args.len() != 2 {
|
||||
return Err(IntrinsicExecutionError::ArityMismatch { expected: 2, got: args.len() });
|
||||
}
|
||||
let x = expect_float_arg(args, 0)?;
|
||||
let y = expect_float_arg(args, 1)?;
|
||||
Ok(vec![Value::Float((x * x + y * y).sqrt())])
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn builtin_lookup_uses_canonical_identity() {
|
||||
let color = lookup_builtin_type("color", 1).expect("color builtin must exist");
|
||||
assert_eq!(color.key(), COLOR);
|
||||
assert!(lookup_builtin_type("Color", 1).is_none());
|
||||
assert!(lookup_builtin_type("color", 2).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn builtin_layout_flattening_matches_expected_widths() {
|
||||
let color = lookup_builtin_type("color", 1).expect("color builtin must exist");
|
||||
assert_eq!(color.flatten_layout(), vec![AbiType::Builtin(COLOR)]);
|
||||
assert_eq!(color.flat_slot_width, 1);
|
||||
|
||||
let vec2 = lookup_builtin_type("vec2", 1).expect("vec2 builtin must exist");
|
||||
assert_eq!(vec2.flatten_layout(), vec![AbiType::Scalar(BuiltinScalarType::Float); 2]);
|
||||
assert_eq!(vec2.flat_slot_width, 2);
|
||||
|
||||
let pixel = lookup_builtin_type("pixel", 1).expect("pixel builtin must exist");
|
||||
assert_eq!(
|
||||
pixel.flatten_layout(),
|
||||
vec![
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Builtin(COLOR),
|
||||
]
|
||||
);
|
||||
assert_eq!(pixel.flat_slot_width, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn builtin_field_offsets_are_stable() {
|
||||
let vec2 = lookup_builtin_type("vec2", 1).expect("vec2 builtin must exist");
|
||||
assert_eq!(vec2.field("x").map(|field| field.start_slot), Some(0));
|
||||
assert_eq!(vec2.field("y").map(|field| field.start_slot), Some(1));
|
||||
|
||||
let pixel = lookup_builtin_type("pixel", 1).expect("pixel builtin must exist");
|
||||
let color_field = pixel.field("color").expect("pixel.color must exist");
|
||||
assert_eq!(color_field.start_slot, 2);
|
||||
assert_eq!(color_field.flat_slot_width, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn builtin_constant_registry_is_separate_from_intrinsics() {
|
||||
let zero = lookup_builtin_constant("vec2", "zero", 1).expect("vec2.zero must exist");
|
||||
assert_eq!(zero.key, BuiltinConstKey::new("vec2", "zero", 1));
|
||||
assert_eq!(zero.flat_slot_width, 2);
|
||||
assert_eq!(zero.flat_slot_layout, &VEC2_ZERO_LAYOUT);
|
||||
assert_eq!(zero.direct_slots(), Some(&VEC2_ZERO_SLOTS[..]));
|
||||
assert!(lookup_intrinsic("vec2", "zero", 1).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn intrinsic_metadata_exposes_arg_and_return_layouts() {
|
||||
let dot = lookup_intrinsic("vec2", "dot", 1).expect("vec2.dot must exist");
|
||||
assert_eq!(dot.arg_layout.len(), 4);
|
||||
assert_eq!(dot.ret_layout.len(), 1);
|
||||
assert!(dot.deterministic);
|
||||
assert!(!dot.may_allocate);
|
||||
|
||||
let length = lookup_intrinsic("vec2", "length", 1).expect("vec2.length must exist");
|
||||
assert_eq!(length.arg_layout.len(), 2);
|
||||
assert_eq!(length.ret_layout.len(), 1);
|
||||
assert_eq!(lookup_intrinsic_by_id(length.id).map(|meta| meta.key()), Some(length.key()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn intrinsic_implementations_are_registered_without_syscalls() {
|
||||
let dot = lookup_intrinsic("vec2", "dot", 1).expect("vec2.dot must exist");
|
||||
let result = (dot.implementation)(&[
|
||||
Value::Float(1.0),
|
||||
Value::Float(2.0),
|
||||
Value::Float(3.0),
|
||||
Value::Float(4.0),
|
||||
])
|
||||
.expect("dot implementation must execute");
|
||||
assert_eq!(result, vec![Value::Float(11.0)]);
|
||||
|
||||
let length = lookup_intrinsic("vec2", "length", 1).expect("vec2.length must exist");
|
||||
let result = (length.implementation)(&[Value::Float(3.0), Value::Float(4.0)])
|
||||
.expect("length implementation must execute");
|
||||
assert_eq!(result, vec![Value::Float(5.0)]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user