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
use cranelift_entity::entity_impl;

use super::*;

/// A handle to a single function block
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Block(u32);
entity_impl!(Block, "block");
impl Default for Block {
    #[inline]
    fn default() -> Self {
        use cranelift_entity::packed_option::ReservedValue;

        Self::reserved_value()
    }
}
impl formatter::PrettyPrint for Block {
    fn render(&self) -> formatter::Document {
        use crate::formatter::*;

        const_text("(") + const_text("block") + const_text(" ") + display(self.0) + const_text(")")
    }
}

/// Data associated with a `Block`.
///
/// Blocks have arguments, and consist of a sequence of instructions.
pub struct BlockData {
    pub id: Block,
    pub params: ValueList,
    pub insts: InstructionList,
}
impl Drop for BlockData {
    fn drop(&mut self) {
        self.insts.fast_clear();
    }
}
impl Clone for BlockData {
    fn clone(&self) -> Self {
        Self {
            id: self.id,
            params: self.params,
            insts: Default::default(),
        }
    }
}
impl BlockData {
    pub(crate) fn new(id: Block) -> Self {
        Self {
            id,
            params: ValueList::new(),
            insts: Default::default(),
        }
    }

    #[inline]
    pub fn arity(&self, pool: &ValueListPool) -> usize {
        self.params.len(pool)
    }

    #[inline]
    pub fn params<'a, 'b: 'a>(&'b self, pool: &'a ValueListPool) -> &'a [Value] {
        self.params.as_slice(pool)
    }

    pub fn insts(&self) -> impl Iterator<Item = Inst> + '_ {
        Insts {
            cursor: self.insts.front(),
        }
    }

    #[inline(always)]
    pub fn prepend(&mut self, inst: UnsafeRef<InstNode>) {
        self.insts.push_front(inst);
    }

    #[inline(always)]
    pub fn append(&mut self, inst: UnsafeRef<InstNode>) {
        self.insts.push_back(inst);
    }

    #[inline(always)]
    pub fn front<'a, 'b: 'a>(&'b self) -> InstructionCursor<'a> {
        self.insts.front()
    }

    #[inline(always)]
    pub fn back<'a, 'b: 'a>(&'b self) -> InstructionCursor<'a> {
        self.insts.back()
    }

    pub fn cursor_at_inst<'a, 'b: 'a>(&'b self, inst: Inst) -> InstructionCursor<'a> {
        let mut cursor = self.insts.front();
        while let Some(current_inst) = cursor.get() {
            if current_inst.key == inst {
                break;
            }
            cursor.move_next();
        }
        cursor
    }

    pub fn cursor_mut_at_inst<'a, 'b: 'a>(&'b mut self, inst: Inst) -> InstructionCursorMut<'a> {
        let mut cursor = self.insts.front_mut();
        while let Some(current_inst) = cursor.get() {
            if current_inst.key == inst {
                break;
            }
            cursor.move_next();
        }
        cursor
    }

    #[inline(always)]
    pub fn cursor_mut<'a, 'b: 'a>(&'b mut self) -> InstructionCursorMut<'a> {
        self.insts.front_mut()
    }

    pub fn cursor_mut_at<'a, 'b: 'a>(&'b mut self, index: usize) -> InstructionCursorMut<'a> {
        let mut cursor = self.insts.front_mut();
        for _ in 0..index {
            cursor.move_next();
            assert!(!cursor.is_null(), "index out of bounds");
        }
        cursor
    }

    #[inline]
    pub fn insert_after(&mut self, index: usize, inst: UnsafeRef<InstNode>) {
        let mut cursor = self.cursor_mut_at(index);
        cursor.insert_after(inst);
    }

    #[inline]
    pub fn insert_before(&mut self, index: usize, inst: UnsafeRef<InstNode>) {
        let mut cursor = self.cursor_mut_at(index);
        cursor.insert_before(inst);
    }

    pub fn first(&self) -> Option<Inst> {
        self.insts.front().get().map(|data| data.key)
    }

    pub fn last(&self) -> Option<Inst> {
        self.insts.back().get().map(|data| data.key)
    }

    pub fn is_empty(&self) -> bool {
        self.insts.is_empty()
    }

    pub fn len(&self) -> usize {
        self.insts.iter().count()
    }
}

struct Insts<'f> {
    cursor: InstructionCursor<'f>,
}
impl<'f> Iterator for Insts<'f> {
    type Item = Inst;

    fn next(&mut self) -> Option<Self::Item> {
        if self.cursor.is_null() {
            return None;
        }
        let next = self.cursor.get().map(|data| data.key);
        self.cursor.move_next();
        next
    }
}