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
use std::iter::zip;

use crate::{
    item_id::ItemId,
    llir_nodes::{FastStore, Node},
    minecraft_utils::ScoreboardValue,
    ObjectRef,
};

/// Copies a scoreboard value from source to destination
#[must_use]
pub fn copy(dest: ItemId, source: ItemId) -> Node {
    Node::FastStore(FastStore {
        id: dest,
        value: ScoreboardValue::Scoreboard(source),
    })
}

/// Copies all items from source over to destination
pub fn mem_copy<F>(mut add_node: F, dest: &ObjectRef, source: &ObjectRef)
where
    F: FnMut(Node),
{
    if source.class.diverges() {
        return;
    }

    let dest_layout = dest.payload.memory_layout();
    let source_layout = source.payload.memory_layout();

    assert_eq!(
        dest_layout.mem_size(),
        source_layout.mem_size(),
        "Layout mismatch"
    );

    match (dest_layout, source_layout) {
        (MemoryLayout::Unsized, MemoryLayout::Unsized) => (),
        (MemoryLayout::One(dest), MemoryLayout::One(source)) => {
            if dest != source {
                add_node(copy(*dest, *source));
            }
        }
        (MemoryLayout::Multiple(dest_vec), MemoryLayout::Multiple(source_vec))
            if dest_vec.len() == source_vec.len() =>
        {
            for (dest, source) in zip(dest_vec, source_vec) {
                if dest != source {
                    add_node(copy(*dest, *source));
                }
            }
        }
        (_, _) => panic!("Incompatible layouts: {dest_layout:?} and {source_layout:?}",),
    }
}

/// Specifies how a specific object lays out its runtime memory
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum MemoryLayout {
    /// This type has no runtime memory
    Unsized,
    /// This type has exactly one integer sized field
    One(ItemId),
    /// This type is spread across multiple fields
    Multiple(Vec<ItemId>),
}

impl MemoryLayout {
    /// Returns the amount of words that this layout occupies
    pub fn mem_size(&self) -> usize {
        match self {
            MemoryLayout::Unsized => 0,
            MemoryLayout::One(_) => 1,
            MemoryLayout::Multiple(words) => words.len(),
        }
    }

    pub fn iter(&self) -> impl Iterator<Item = &ItemId> {
        let mut one_iter = None;
        let mut multiple_iter = None;
        match self {
            MemoryLayout::Unsized => {}
            MemoryLayout::One(item_id) => one_iter = Some(item_id),
            MemoryLayout::Multiple(item_ids) => multiple_iter = Some(item_ids.iter()),
        };
        one_iter
            .into_iter()
            .chain(multiple_iter.into_iter().flatten())
    }
}

impl From<Vec<ItemId>> for MemoryLayout {
    fn from(ids: Vec<ItemId>) -> Self {
        match ids.as_slice() {
            [] => MemoryLayout::Unsized,
            [one] => MemoryLayout::One(*one),
            _ => MemoryLayout::Multiple(ids),
        }
    }
}

impl<'a> FromIterator<&'a MemoryLayout> for MemoryLayout {
    fn from_iter<T: IntoIterator<Item = &'a MemoryLayout>>(iter: T) -> Self {
        let mut iter = iter.into_iter();
        let first = iter.next();
        match first {
            None => MemoryLayout::Unsized,
            Some(first) => {
                let second = iter.next();
                match second {
                    None => first.clone(),
                    Some(second) => {
                        let mut items = Vec::with_capacity(first.mem_size() + second.mem_size());
                        items.extend(first.iter().copied());
                        items.extend(second.iter().copied());
                        items.extend(iter.flat_map(MemoryLayout::iter).copied());
                        MemoryLayout::Multiple(items)
                    }
                }
            }
        }
    }
}