1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
#![warn(missing_docs)]

//! Boxed values and heaps
//!
//! This contains the implementation of our garbage collector and the types it can manage. Some
//! types (such as `Int` and `Float`) have corresponding unboxed representations and are only boxed
//! for the purposes of runtime dynamic typing. Complex values (such as `Vector` and `Sym`) have no
//! unboxed representation.
//!
//! Boxes can also be placed on the stack on in static constants instead of the heap. This is of
//! limited value to Rust code but is frequently used by the compiler to avoid the overhead of
//! allocation and garbage collection.

mod heap;
pub mod refs;
mod types;

use std::hash::{Hash, Hasher};
use std::{fmt, ptr};

use crate::abitype::{BoxedAbiType, EncodeBoxedAbiType};
use crate::boxed::refs::Gc;

pub use crate::boxed::heap::{collect, type_info};
pub use crate::boxed::heap::{AsHeap, Heap};
pub use crate::boxed::types::char::Char;
pub use crate::boxed::types::field_value::{FieldValue, FieldValueIter};
pub use crate::boxed::types::float::Float;
pub use crate::boxed::types::fun::{Captures, FunThunk, ThunkEntry};
pub use crate::boxed::types::int::Int;
pub use crate::boxed::types::list::{List, ListSubtype, Nil, Pair, NIL_INSTANCE};
pub use crate::boxed::types::map::Map;
pub use crate::boxed::types::record::{Record, RecordClassId, RecordStorage};
pub use crate::boxed::types::record_data::RecordData;
pub use crate::boxed::types::set::Set;
pub use crate::boxed::types::str::{Str, StrStorage};
pub use crate::boxed::types::sym::Sym;
pub use crate::boxed::types::vector::Vector;

/// Prelude of common traits useful for working with boxed values
pub mod prelude {
    pub use super::AsHeap;
    pub use super::Boxed;
    pub use super::DistinctTagged;
    pub use super::HashInHeap;
    pub use super::PartialEqInHeap;
}

/// Size of a boxed value in bytes
#[derive(PartialEq, Debug, Copy, Clone)]
pub enum BoxSize {
    /// 16 byte boxed value
    Size16,
    /// 32 byte boxed value
    Size32,
}

impl BoxSize {
    /// Returns the number of 16 byte cells required by this box size
    pub fn cell_count(self) -> usize {
        match self {
            BoxSize::Size16 => 1,
            BoxSize::Size32 => 2,
        }
    }

    /// Returns the corresponding `AllocType` if this box was allocated on the heap
    pub fn to_heap_alloc_type(self) -> AllocType {
        match self {
            BoxSize::Size16 => AllocType::Heap16,
            BoxSize::Size32 => AllocType::Heap32,
        }
    }
}

/// Allocation type for boxed values
#[repr(u8)]
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum AllocType {
    /// Static constant value
    Const,
    /// Stack allocated value of unknown length
    Stack,
    /// Heap allocated 16 byte value
    Heap16,
    /// Heap allocated 32 byte value
    Heap32,

    /// Box pointing to a new 16 byte heap location
    ///
    /// This is a temporary type used during garbage collection.
    HeapForward16,

    /// Box pointing to a new 32 byte heap location
    ///
    /// This is a temporary type used during garbage collection.
    HeapForward32,
}

impl AllocType {
    /// Returns the corresponding `BoxSize` if this type is heap allocated
    pub fn to_heap_box_size(self) -> Option<BoxSize> {
        match self {
            AllocType::Heap16 => Some(BoxSize::Size16),
            AllocType::Heap32 => Some(BoxSize::Size32),
            _ => None,
        }
    }
}

/// Header for common boxed value metadata
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct Header {
    type_tag: TypeTag,
    alloc_type: AllocType,
}

impl Header {
    /// Returns a new header for the given type tag and allocation type
    pub fn new(type_tag: TypeTag, alloc_type: AllocType) -> Header {
        Header {
            type_tag,
            alloc_type,
        }
    }

    /// Returns the constant type tag for this value
    pub fn type_tag(self) -> TypeTag {
        self.type_tag
    }

    /// Return the allocation type for this value
    pub fn alloc_type(self) -> AllocType {
        self.alloc_type
    }
}

/// Equivalent of [`PartialEq`] that receives an additional [`Heap`] parameter
///
/// This is required for types that require additional metadata from the heap to perform equality
/// checks.
pub trait PartialEqInHeap {
    /// Returns true if the values are equal
    ///
    /// Both values will be in the same heap.
    fn eq_in_heap(&self, heap: &Heap, other: &Self) -> bool;
}

impl<T> PartialEqInHeap for T
where
    T: PartialEq,
{
    fn eq_in_heap(&self, _heap: &Heap, other: &Self) -> bool {
        self.eq(other)
    }
}

/// Equivalent of [`Hash`] that receives an additional [`Heap`] parameter
///
/// This is required for types that require additional metadata from the heap to calculate hashes.
pub trait HashInHeap {
    /// Feeds this value into the given [`Hasher`]
    fn hash_in_heap<H: Hasher>(&self, heap: &Heap, state: &mut H);
}

impl<T> HashInHeap for T
where
    T: Hash,
{
    fn hash_in_heap<H: Hasher>(&self, _heap: &Heap, state: &mut H) {
        self.hash(state)
    }
}

/// Boxed value
///
/// Boxes can be allocated on the stack, heap or a static constant. Every box is tagged with a
/// top-level type.
pub trait Boxed: Sized + PartialEqInHeap + HashInHeap + fmt::Debug {
    /// Casts this value to an `Any` reference
    fn as_any_ref(&self) -> Gc<Any> {
        unsafe { Gc::new(&*(self as *const Self as *const Any)) }
    }

    /// Returns the header of the box
    fn header(&self) -> Header {
        self.as_any_ref().header
    }
}

impl EncodeBoxedAbiType for Any {
    const BOXED_ABI_TYPE: BoxedAbiType = BoxedAbiType::Any;
}

/// Marks that this boxed struct has a specific constant type tag
///
/// For example, [`Vector<Str>`] is `ConstTagged` because it always has a type tag of `Vector`. As
/// a counterexample, [`Num`] is not because it could either have an `Int` or `Float` type tag.
///
/// In mathematical terms this can be thought of as the struct being surjective to the type tag.
pub trait ConstTagged: Boxed {
    /// Type tag for values of this type
    const TYPE_TAG: TypeTag;
}

/// Indicates that this boxed struct does not share type tags with unrelated types
///
/// For example, [`Num`] is `DistinctTagged` because it only shares type tags with `Any`, `Float`
/// and `Int` which are all either subtypes or supertypes. As a counterexample, [`Vector<Str>`] is
/// not because it shares a type tag with [`Vector<Sym>`].
///
/// In mathematical terms this can be thought of as the struct being injective to the type tag
pub trait DistinctTagged: Boxed {
    /// Returns if the passed type tag corresponds to this type
    fn has_tag(type_tag: TypeTag) -> bool;
}

/// Marks that every boxed value with `TYPE_TAG` corresponds to this boxed struct
///
/// For example, [`Str`] is `UniqueTagged` because no other struct has the type tag of `Str`. As a
/// counterexample, `Vector<Str>` is not because it shares a type tag with `Vector<Sym>`.
///
/// In mathematical terms this can be thought of as the struct being bijective with the type tag.
pub trait UniqueTagged: ConstTagged + DistinctTagged {}

impl<T: UniqueTagged> EncodeBoxedAbiType for T {
    const BOXED_ABI_TYPE: BoxedAbiType = BoxedAbiType::UniqueTagged(T::TYPE_TAG);
}

macro_rules! define_const_tagged_boxes {
    ($($name:ident),*) => {
        /// Tag byte identifying top-level types
        #[repr(u8)]
        #[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
        pub enum TypeTag {
            $(
                #[allow(missing_docs)]
                $name
            ),*
        }

        /// Static list of all possible type tags
        ///
        /// This is guaranteed to be sorted
        pub const ALL_TYPE_TAGS: &'static [TypeTag] = &[
            $( TypeTag::$name ),*
        ];

        impl TypeTag {
            /// Returns a string representation for the type
            pub fn to_str(self) -> &'static str {
                match self {
                    $(
                        TypeTag::$name => {
                            stringify!($name)
                        }
                    )*
                }
            }
        }

        $(
            impl ConstTagged for $name {
                const TYPE_TAG: TypeTag = TypeTag::$name;
            }

            impl DistinctTagged for $name {
                fn has_tag(type_tag: TypeTag) -> bool {
                    Self::TYPE_TAG == type_tag
                }
            }
        )*

        define_supertype!(
            /// Supertype of all boxed types
            Any,
            AnySubtype, DistinctTagged, as_any_ref, { $($name),* });
    }
}

impl TypeTag {
    /// Returns the boxed ABI type corresponding to this type tag
    pub fn to_boxed_abi_type(self) -> BoxedAbiType {
        BoxedAbiType::UniqueTagged(self)
    }

    /// Returns a header for a constant boxed values of this type
    pub fn to_const_header(self) -> Header {
        Header::new(self, AllocType::Const)
    }

    /// Returns a header for heap allocated values of this type and size
    pub fn to_heap_header(self, box_size: BoxSize) -> Header {
        Header::new(self, box_size.to_heap_alloc_type())
    }
}

macro_rules! define_singleton_box {
    (
        $(#[$struct_docs:meta])*
        $type_name:ident,
        $(#[$static_docs:meta])*
        $static_name:ident,
        $export_name:expr
    ) => {
        $(#[$struct_docs])*
        #[repr(C, align(16))]
        #[derive(Debug)]
        pub struct $type_name {
            header: Header,
        }

        impl Boxed for $type_name {}
        impl UniqueTagged for $type_name {}

        $(#[$static_docs])*
        #[export_name = $export_name]
        pub static $static_name: $type_name = $type_name {
            header: Header {
                type_tag: $type_name::TYPE_TAG,
                alloc_type: AllocType::Const,
            },
        };

        impl PartialEq for $type_name {
            fn eq(&self, _: &$type_name) -> bool {
                // This is tricky - we're a singleton so if the types match we must be equal
                true
            }
        }

        impl Hash for $type_name {
            fn hash<H: Hasher>(&self, state: &mut H) {
                Self::TYPE_TAG.hash(state);
                state.write_usize(&$static_name as *const $type_name as usize);
            }
        }
    };
}

macro_rules! define_supertype {
    (
        $(#[$docs:meta])*
        $name:ident,
        $subtype_enum:ident, $subtype_trait:ident, $as_enum_ref:ident, { $($member:ident),* }
    ) => {
        $(#[$docs])*
        #[repr(C, align(16))]
        pub struct $name {
            header: Header,
        }

        impl Boxed for $name {}

        impl DistinctTagged for $name {
            fn has_tag(type_tag: TypeTag) -> bool {
                [$( TypeTag::$member ),*].contains(&type_tag)
            }
        }

        impl $name {
            /// Returns a subtype of this value based on its type tag
            pub fn as_subtype(&self) -> $subtype_enum<'_> {
                #[allow(unreachable_patterns)]
                match self.header.type_tag {
                    $(
                        TypeTag::$member => {
                            $subtype_enum::$member(unsafe {
                                &*(self as *const $name as *const $member)
                            })
                        }
                    )*
                    other => {
                        unreachable!("Unexpected type tag: {:?}", other);
                    }
                }
            }

            /// Tries to downcast this reference to a subtype based on its type tag
            pub fn downcast_ref<T: $subtype_trait>(&self) -> Option<Gc<T>>
            {
                if T::has_tag(self.header.type_tag) {
                    Some(unsafe { Gc::new(&*(self as *const $name as *const T)) })
                } else {
                    None
                }
            }
        }

        impl HashInHeap for $name {
            fn hash_in_heap<H: Hasher>(&self, heap: &Heap, state: &mut H) {
                match self.as_subtype() {
                    $(
                        $subtype_enum::$member(subtype) => {
                            subtype.hash_in_heap(heap, state)
                        }
                    )*
                }
            }
        }

        impl PartialEqInHeap for $name {
            fn eq_in_heap(&self, heap: &Heap, other: &$name) -> bool {
                match (self.as_subtype(), other.as_subtype()) {
                    $(
                        ($subtype_enum::$member(self_value), $subtype_enum::$member(other_value)) => {
                            self_value.eq_in_heap(heap, other_value)
                        }
                    ),*
                    _ => false
                }
            }
        }

        impl fmt::Debug for $name {
            fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
                match self.as_subtype() {
                    $(
                        $subtype_enum::$member(subtype) => {
                            subtype.fmt(formatter)
                        }
                    )*
                }
            }
        }

        impl Drop for $name {
            fn drop(&mut self) {
                // Cast to the correct type so Rust knows which Drop implementation to call
                match self.as_subtype() {
                    $(
                        $subtype_enum::$member(subtype) => {
                            unsafe {
                                ptr::drop_in_place(subtype as *const $member as *mut $member);
                            }
                        }
                    )*
                }
            }
        }

        /// Possible subtypes of this supertype
        #[derive(Debug)]
        pub enum $subtype_enum<'a> {
            $(
                #[allow(missing_docs)]
                $member(&'a $member)
            ),*
        }
    }
}

macro_rules! define_tagged_union {
    (
        $(#[$struct_docs:meta])*
        $name:ident,
        $(#[$subtype_docs:meta])*
        $subtype_enum:ident,
        $subtype_trait:ident, $as_enum_ref:ident, { $($member:ident),* }
    ) => {
        define_supertype!(
            $(#[$struct_docs])*
            $name,
            $subtype_enum, $subtype_trait, $as_enum_ref, { $($member),* }
        );

        $(#[$subtype_docs])*
        pub trait $subtype_trait : DistinctTagged {}

        $(
            impl $member {
                /// Casts this value to its supertype
                pub fn $as_enum_ref(&self) -> Gc<$name> {
                    unsafe { Gc::new(&*(self as *const Self as *const $name)) }
                }
            }

            impl $subtype_trait for $member {}
        )*

        impl EncodeBoxedAbiType for $name {
            const BOXED_ABI_TYPE: BoxedAbiType = BoxedAbiType::Union(stringify!($name), &[
                $( $member::TYPE_TAG ),*
            ]);
        }
    };
}

define_const_tagged_boxes! {
    Float,
    Int,
    Char,
    Str,
    Sym,
    Pair,
    Nil,
    True,
    False,
    Vector,
    FunThunk,
    Record,
    Set,
    Map
}

define_singleton_box!(
    /// Boolean true
    True,
    /// Static constant instance of [`True`]
    TRUE_INSTANCE,
    "ARRET_TRUE"
);

define_singleton_box!(
    /// Boolean false
    False,
    /// Static constant instance of [`False`]
    FALSE_INSTANCE,
    "ARRET_FALSE"
);

define_tagged_union!(
    /// Union of numeric types
    Num,
    /// Possible subtypes of [`Num`]
    NumSubtype,
    NumMember, as_num_ref, {
        Int,
        Float
    }
);

define_tagged_union!(
    /// Union of boolean types
    Bool,
    /// Possible subtypes of [`Bool`]
    BoolSubtype,
    BoolMember, as_bool_ref, {
        True,
        False
    }
);

impl Bool {
    /// Returns the singleton box corresponding the boolean value
    pub fn singleton_ref(value: bool) -> Gc<Bool> {
        if value {
            TRUE_INSTANCE.as_bool_ref()
        } else {
            FALSE_INSTANCE.as_bool_ref()
        }
    }

    /// Returns the unboxed value of this boolean
    pub fn as_bool(&self) -> bool {
        match self.as_subtype() {
            BoolSubtype::True(_) => true,
            BoolSubtype::False(_) => false,
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use std::mem;

    #[test]
    fn sizes() {
        assert_eq!(2, mem::size_of::<Header>());
        assert_eq!(16, mem::size_of::<Nil>());
        assert_eq!(16, mem::size_of::<True>());
        assert_eq!(16, mem::size_of::<False>());
    }

    #[test]
    fn downcast_ref() {
        let mut heap = Heap::empty();

        let box_float = Float::new(&mut heap, 2.0);
        let box_float_as_any = box_float.as_any_ref();

        assert!(!box_float_as_any.downcast_ref::<Int>().is_some());
        assert!(box_float_as_any.downcast_ref::<Float>().is_some());
    }

    #[test]
    fn as_tagged() {
        let mut heap = Heap::empty();

        let box_float = Float::new(&mut heap, 2.0);
        let box_float_as_any = box_float.as_any_ref();

        if let AnySubtype::Float(_) = box_float_as_any.as_subtype() {
        } else {
            panic!("Failed to get tagged representation")
        }
    }

    #[test]
    fn any_equality() {
        let mut heap = Heap::empty();

        let box_two = Float::new(&mut heap, 2.0);
        let box_two_as_any = box_two.as_any_ref();

        let box_three = Float::new(&mut heap, 3.0);
        let box_three_as_any = box_three.as_any_ref();

        assert!(box_two_as_any.eq_in_heap(&heap, &box_two_as_any));
        assert!(!box_two_as_any.eq_in_heap(&heap, &box_three_as_any));

        #[allow(clippy::eq_op)]
        {
            assert_eq!(TRUE_INSTANCE, TRUE_INSTANCE);
        }
    }

    #[test]
    fn any_fmt_debug() {
        let mut heap = Heap::empty();

        let boxed_one = Int::new(&mut heap, 1);
        let boxed_one_as_any = boxed_one.as_any_ref();
        assert_eq!("Int(1)", format!("{:?}", boxed_one_as_any));
    }

    #[test]
    fn union_types() {
        let mut heap = Heap::empty();

        let box_float = Float::new(&mut heap, 2.0);
        let box_float_as_any = box_float.as_any_ref();

        if let Some(stack_num) = box_float_as_any.downcast_ref::<Num>() {
            if let NumSubtype::Float(_) = stack_num.as_subtype() {
            } else {
                panic!("Couldn't get tagged Float from Num");
            }

            assert!(!stack_num.downcast_ref::<Int>().is_some());
            assert!(stack_num.downcast_ref::<Float>().is_some());
        } else {
            panic!("Float was not a Num");
        }

        let box_str = Str::new(&mut heap, "Test!");
        let box_str_as_any = box_str.as_any_ref();

        assert!(!box_str_as_any.downcast_ref::<Num>().is_some());
    }
}