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

use llvm_sys::prelude::*;

use arret_runtime::boxed;

use crate::mir::ops;

pub mod core;
pub mod plan;
pub mod types;

/// Indicates where memory for a box allocation should come from
#[derive(PartialEq, Debug, Clone, Copy)]
pub enum BoxSource {
    Stack,
    Heap(boxed::BoxSize),
}

/// Contains the sub-plans for a conditional branch
#[derive(PartialEq, Debug)]
pub struct CondPlan<'op> {
    pub true_subplan: Vec<AllocAtom<'op>>,
    pub false_subplan: Vec<AllocAtom<'op>>,
}

/// Represents a sequence of MIR ops that begin and end with the heap in a consistent state
#[derive(PartialEq, Debug, Default)]
pub struct AllocAtom<'op> {
    box_sources: Vec<BoxSource>,
    cond_plans: Vec<CondPlan<'op>>,

    ops_base: &'op [ops::Op],
    ops_count: usize,
}

impl<'op> AllocAtom<'op> {
    /// Creates a new `AllocAtom` with its ops starting at the specified slice
    fn new(ops_base: &'op [ops::Op]) -> Self {
        Self {
            ops_base,
            ..Default::default()
        }
    }

    pub fn ops(&self) -> &'op [ops::Op] {
        &self.ops_base[0..self.ops_count]
    }

    /// Increments the used size of our ops by one
    fn push_op(&mut self) {
        self.ops_count += 1
    }

    fn is_empty(&self) -> bool {
        self.ops_count == 0
    }
}

pub struct ActiveAlloc<'op> {
    box_slots: LLVMValueRef,
    total_cells: usize,
    used_cells: usize,

    box_source_iter: vec::IntoIter<BoxSource>,
    cond_plan_iter: vec::IntoIter<CondPlan<'op>>,
}

impl<'op> ActiveAlloc<'op> {
    pub fn is_empty(&self) -> bool {
        self.total_cells == self.used_cells
    }

    pub fn next_box_source(&mut self) -> BoxSource {
        self.box_source_iter.next().unwrap()
    }

    pub fn next_cond_plan(&mut self) -> CondPlan<'op> {
        self.cond_plan_iter.next().unwrap()
    }
}