1use crate::ast::SlotShape;
21use crate::ast::{PortType, Value};
22
23pub(crate) fn borrow_pair(v: &Value) -> Option<(u64, u64)> {
28 Some(match v {
29 Value::Str(s) => (s.as_ptr() as usize as u64, s.len() as u64),
30 Value::Bytes(b) => (b.as_ptr() as usize as u64, b.len() as u64),
31 Value::Json(_) | Value::Ext(_) | Value::Handle(_) => (v as *const Value as usize as u64, 1),
32 Value::VecF32(v) => slice_pair(v.as_slice()),
33 Value::VecF64(v) => slice_pair(v.as_slice()),
34 Value::VecF16(v) => slice_pair(v.as_slice()),
35 Value::VecI8(v) => slice_pair(v.as_slice()),
36 Value::VecI16(v) => slice_pair(v.as_slice()),
37 Value::VecI32(v) => slice_pair(v.as_slice()),
38 Value::VecI64(v) => slice_pair(v.as_slice()),
39 _ => return None,
40 })
41}
42
43#[inline]
45pub(crate) fn slice_pair<T>(s: &[T]) -> (u64, u64) {
46 (s.as_ptr() as usize as u64, s.len() as u64)
47}
48
49#[inline]
52pub(crate) fn empty_pair() -> (u64, u64) {
53 (
54 std::ptr::NonNull::<u8>::dangling().as_ptr() as usize as u64,
55 0,
56 )
57}
58
59#[cfg(feature = "jit")]
63pub(crate) fn encode_slots(v: &Value, out: &mut [u64]) -> Option<()> {
64 match v {
65 Value::U64(x) => out[0] = *x,
66 Value::I64(x) => out[0] = *x as u64,
67 Value::F64(x) => out[0] = x.to_bits(),
68 Value::Bool(b) => out[0] = *b as u64,
69 _ => {
70 let (p, l) = borrow_pair(v)?;
71 out[0] = p;
72 out[1] = l;
73 }
74 }
75 Some(())
76}
77
78pub(crate) unsafe fn decode_pair(ty: PortType, ptr: u64, len: u64) -> Value {
88 use crate::ast::SliceArc;
89 let (p, n) = (ptr as usize, len as usize);
90 unsafe {
92 match ty {
93 PortType::Str => Value::Str(std::sync::Arc::from(std::str::from_utf8_unchecked(
94 std::slice::from_raw_parts(p as *const u8, n),
95 ))),
96 PortType::Bytes => Value::Bytes(std::sync::Arc::from(std::slice::from_raw_parts(
97 p as *const u8,
98 n,
99 ))),
100 PortType::Json | PortType::Ext | PortType::Handle => {
101 if n == 0 {
102 Value::None
103 } else {
104 (*(p as *const Value)).clone()
105 }
106 }
107 PortType::VecF32 => Value::VecF32(SliceArc::from_vec(
108 std::slice::from_raw_parts(p as *const f32, n).to_vec(),
109 )),
110 PortType::VecF64 => Value::VecF64(SliceArc::from_vec(
111 std::slice::from_raw_parts(p as *const f64, n).to_vec(),
112 )),
113 PortType::VecF16 => Value::VecF16(SliceArc::from_vec(
114 std::slice::from_raw_parts(p as *const half::f16, n).to_vec(),
115 )),
116 PortType::VecI8 => Value::VecI8(SliceArc::from_vec(
117 std::slice::from_raw_parts(p as *const i8, n).to_vec(),
118 )),
119 PortType::VecI16 => Value::VecI16(SliceArc::from_vec(
120 std::slice::from_raw_parts(p as *const i16, n).to_vec(),
121 )),
122 PortType::VecI32 => Value::VecI32(SliceArc::from_vec(
123 std::slice::from_raw_parts(p as *const i32, n).to_vec(),
124 )),
125 PortType::VecI64 => Value::VecI64(SliceArc::from_vec(
126 std::slice::from_raw_parts(p as *const i64, n).to_vec(),
127 )),
128 other => panic!("{other:?} is not a Ref2-colored port type"),
129 }
130 }
131}
132
133pub(crate) fn decode_slot(slots: &[u64], ty: PortType) -> Value {
136 match ty {
137 PortType::F64 => Value::F64(f64::from_bits(slots[0])),
138 PortType::Bool => Value::Bool(slots[0] != 0),
139 PortType::I64 => Value::I64(slots[0] as i64),
140 PortType::I8 | PortType::I16 | PortType::I32 => Value::I64(slots[0] as i64),
143 ty if ty.slot_color() == crate::ast::SlotColor::Ref2 => unsafe {
146 decode_pair(ty, slots[0], slots[1])
147 },
148 _ => Value::U64(slots[0]),
149 }
150}
151
152pub fn decode_output(buffer: &[u64], slot: usize, ty: PortType) -> Value {
158 use crate::ast::{Bits128, RegLanes, SlotColor};
159 if ty.slot_color() == SlotColor::Imm2 {
160 let limbs = Bits128([buffer[slot], buffer[slot + 1]]);
161 return match ty {
162 PortType::U128 => Value::U128(limbs),
163 PortType::I128 => Value::I128(limbs),
164 PortType::RegI8x16 => Value::Reg128(limbs, RegLanes::I8x16),
165 PortType::RegI16x8 => Value::Reg128(limbs, RegLanes::I16x8),
166 PortType::RegI32x4 => Value::Reg128(limbs, RegLanes::I32x4),
167 PortType::RegI64x2 => Value::Reg128(limbs, RegLanes::I64x2),
168 PortType::RegF16x8 => Value::Reg128(limbs, RegLanes::F16x8),
169 PortType::RegF32x4 => Value::Reg128(limbs, RegLanes::F32x4),
170 PortType::RegF64x2 => Value::Reg128(limbs, RegLanes::F64x2),
171 _ => Value::Reg128(limbs, RegLanes::Raw),
172 };
173 }
174 decode_slot(&buffer[slot..], ty)
175}
176
177pub unsafe fn arg_ref<'a>(ty: PortType, slots: &'a [u64]) -> crate::ast::ValueRef<'a> {
185 use crate::ast::ValueRef;
186 let (p, n) = (
187 slots.first().copied().unwrap_or(0) as usize,
188 slots.get(1).copied().unwrap_or(0) as usize,
189 );
190 unsafe {
192 match ty {
193 PortType::U64 => ValueRef::U64(slots[0]),
194 PortType::I64 | PortType::I8 | PortType::I16 | PortType::I32 => {
195 ValueRef::I64(slots[0] as i64)
196 }
197 PortType::F64 => ValueRef::F64(f64::from_bits(slots[0])),
198 PortType::Bool => ValueRef::Bool(slots[0] != 0),
199 PortType::Str => ValueRef::Str(std::str::from_utf8_unchecked(
200 std::slice::from_raw_parts(p as *const u8, n),
201 )),
202 PortType::Bytes => ValueRef::Bytes(std::slice::from_raw_parts(p as *const u8, n)),
203 PortType::Json | PortType::Ext | PortType::Handle => {
204 if n == 0 {
205 ValueRef::None
206 } else {
207 ValueRef::from(&*(p as *const Value))
208 }
209 }
210 _ => ValueRef::U64(slots[0]),
211 }
212 }
213}